1173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky//===-- DeclLookups.h - Low-level interface to all names in a DC-*- C++ -*-===//
2173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky//
3173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky//                     The LLVM Compiler Infrastructure
4173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky//
5173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky// This file is distributed under the University of Illinois Open Source
6173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky// License. See LICENSE.TXT for details.
7173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky//
8173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky//===----------------------------------------------------------------------===//
9173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky//
10173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky//  This file defines DeclContext::all_lookups_iterator.
11173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky//
12173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky//===----------------------------------------------------------------------===//
13173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
14173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky#ifndef LLVM_CLANG_AST_DECLLOOKUPS_H
15173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky#define LLVM_CLANG_AST_DECLLOOKUPS_H
16173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
179946fc735d7285f2195f89635370f534afd9877eDmitri Gribenko#include "clang/AST/ASTContext.h"
18173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky#include "clang/AST/DeclBase.h"
19173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky#include "clang/AST/DeclContextInternals.h"
20173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky#include "clang/AST/DeclarationName.h"
21173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
22173a37a57b79bd8f94b85c2273039e760b159922Nick Lewyckynamespace clang {
23173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
24173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky/// all_lookups_iterator - An iterator that provides a view over the results
25173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky/// of looking up every possible name.
26173a37a57b79bd8f94b85c2273039e760b159922Nick Lewyckyclass DeclContext::all_lookups_iterator {
27173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  StoredDeclsMap::iterator It, End;
28173a37a57b79bd8f94b85c2273039e760b159922Nick Lewyckypublic:
29173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  typedef lookup_result             value_type;
30173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  typedef lookup_result             reference;
31173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  typedef lookup_result             pointer;
32173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  typedef std::forward_iterator_tag iterator_category;
33173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  typedef std::ptrdiff_t            difference_type;
34173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
35173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  all_lookups_iterator() {}
36173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  all_lookups_iterator(StoredDeclsMap::iterator It,
37173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky                       StoredDeclsMap::iterator End)
38173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky      : It(It), End(End) {}
39173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
406e322c08b187ba93019732d2295c84c339813e0dRichard Smith  DeclarationName getLookupName() const { return It->first; }
416e322c08b187ba93019732d2295c84c339813e0dRichard Smith
42173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  reference operator*() const { return It->second.getLookupResult(); }
43173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  pointer operator->() const { return It->second.getLookupResult(); }
44173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
45173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  all_lookups_iterator& operator++() {
46173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky    // Filter out using directives. They don't belong as results from name
47173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky    // lookup anyways, except as an implementation detail. Users of the API
48173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky    // should not expect to get them (or worse, rely on it).
49173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky    do {
50173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky      ++It;
51173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky    } while (It != End &&
52173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky             It->first == DeclarationName::getUsingDirectiveName());
53173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
54173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky    return *this;
55173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  }
56173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
57173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  all_lookups_iterator operator++(int) {
58173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky    all_lookups_iterator tmp(*this);
59173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky    ++(*this);
60173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky    return tmp;
61173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  }
62173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
63173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  friend bool operator==(all_lookups_iterator x, all_lookups_iterator y) {
64173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky    return x.It == y.It;
65173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  }
66173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  friend bool operator!=(all_lookups_iterator x, all_lookups_iterator y) {
67173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky    return x.It != y.It;
68173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  }
69173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky};
70173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
71651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesinline DeclContext::lookups_range DeclContext::lookups() const {
72173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  DeclContext *Primary = const_cast<DeclContext*>(this)->getPrimaryContext();
73fb4306756208118d66f63abdd82cc10b19b5e4baDouglas Gregor  if (Primary->hasExternalVisibleStorage())
74b346d2f419ec7d7ce6b20780d518490338efa7deNick Lewycky    getParentASTContext().getExternalSource()->completeVisibleDeclsMap(Primary);
75173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  if (StoredDeclsMap *Map = Primary->buildLookup())
76651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return lookups_range(all_lookups_iterator(Map->begin(), Map->end()),
77651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         all_lookups_iterator(Map->end(), Map->end()));
780e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
790e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // Synthesize an empty range. This requires that two default constructed
800e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // versions of these iterators form a valid empty range.
810e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  return lookups_range(all_lookups_iterator(), all_lookups_iterator());
82651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
83651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
84651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesinline DeclContext::all_lookups_iterator DeclContext::lookups_begin() const {
85651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return lookups().begin();
86173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky}
87173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
886e322c08b187ba93019732d2295c84c339813e0dRichard Smithinline DeclContext::all_lookups_iterator DeclContext::lookups_end() const {
89651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return lookups().end();
90651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
91651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
92651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesinline DeclContext::lookups_range DeclContext::noload_lookups() const {
93173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky  DeclContext *Primary = const_cast<DeclContext*>(this)->getPrimaryContext();
94651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (StoredDeclsMap *Map = Primary->getLookupPtr())
95651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return lookups_range(all_lookups_iterator(Map->begin(), Map->end()),
96651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         all_lookups_iterator(Map->end(), Map->end()));
970e2c34f92f00628d48968dfea096d36381f494cbStephen Hines
980e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // Synthesize an empty range. This requires that two default constructed
990e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  // versions of these iterators form a valid empty range.
1000e2c34f92f00628d48968dfea096d36381f494cbStephen Hines  return lookups_range(all_lookups_iterator(), all_lookups_iterator());
101173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky}
102173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
1036e322c08b187ba93019732d2295c84c339813e0dRichard Smithinline
1046e322c08b187ba93019732d2295c84c339813e0dRichard SmithDeclContext::all_lookups_iterator DeclContext::noload_lookups_begin() const {
105651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return noload_lookups().begin();
1066e322c08b187ba93019732d2295c84c339813e0dRichard Smith}
1076e322c08b187ba93019732d2295c84c339813e0dRichard Smith
1086e322c08b187ba93019732d2295c84c339813e0dRichard Smithinline
1096e322c08b187ba93019732d2295c84c339813e0dRichard SmithDeclContext::all_lookups_iterator DeclContext::noload_lookups_end() const {
110651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  return noload_lookups().end();
1116e322c08b187ba93019732d2295c84c339813e0dRichard Smith}
1126e322c08b187ba93019732d2295c84c339813e0dRichard Smith
113173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky} // end namespace clang
114173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky
115173a37a57b79bd8f94b85c2273039e760b159922Nick Lewycky#endif
116