1249261Sdim//===-- ASTUnresolvedSet.h - Unresolved sets of declarations  ---*- C++ -*-===//
2249261Sdim//
3249261Sdim//                     The LLVM Compiler Infrastructure
4249261Sdim//
5249261Sdim// This file is distributed under the University of Illinois Open Source
6249261Sdim// License. See LICENSE.TXT for details.
7249261Sdim//
8249261Sdim//===----------------------------------------------------------------------===//
9249261Sdim//
10249261Sdim//  This file provides an UnresolvedSet-like class, whose contents are
11249261Sdim//  allocated using the allocator associated with an ASTContext.
12249261Sdim//
13249261Sdim//===----------------------------------------------------------------------===//
14249261Sdim
15249261Sdim#ifndef LLVM_CLANG_AST_ASTUNRESOLVEDSET_H
16249261Sdim#define LLVM_CLANG_AST_ASTUNRESOLVEDSET_H
17249261Sdim
18249261Sdim#include "clang/AST/ASTVector.h"
19249261Sdim#include "clang/AST/UnresolvedSet.h"
20249261Sdim
21249261Sdimnamespace clang {
22249261Sdim
23249261Sdim/// \brief An UnresolvedSet-like class which uses the ASTContext's allocator.
24249261Sdimclass ASTUnresolvedSet {
25263509Sdim  struct DeclsTy : ASTVector<DeclAccessPair> {
26263509Sdim    DeclsTy() {}
27263509Sdim    DeclsTy(ASTContext &C, unsigned N) : ASTVector<DeclAccessPair>(C, N) {}
28263509Sdim
29263509Sdim    bool isLazy() const { return getTag(); }
30263509Sdim    void setLazy(bool Lazy) { setTag(Lazy); }
31263509Sdim  };
32263509Sdim
33249261Sdim  DeclsTy Decls;
34249261Sdim
35249261Sdim  ASTUnresolvedSet(const ASTUnresolvedSet &) LLVM_DELETED_FUNCTION;
36249261Sdim  void operator=(const ASTUnresolvedSet &) LLVM_DELETED_FUNCTION;
37249261Sdim
38263509Sdim  friend class LazyASTUnresolvedSet;
39263509Sdim
40249261Sdimpublic:
41249261Sdim  ASTUnresolvedSet() {}
42249261Sdim  ASTUnresolvedSet(ASTContext &C, unsigned N) : Decls(C, N) {}
43249261Sdim
44249261Sdim  typedef UnresolvedSetIterator iterator;
45249261Sdim  typedef UnresolvedSetIterator const_iterator;
46249261Sdim
47249261Sdim  iterator begin() { return iterator(Decls.begin()); }
48249261Sdim  iterator end() { return iterator(Decls.end()); }
49249261Sdim
50249261Sdim  const_iterator begin() const { return const_iterator(Decls.begin()); }
51249261Sdim  const_iterator end() const { return const_iterator(Decls.end()); }
52249261Sdim
53249261Sdim  void addDecl(ASTContext &C, NamedDecl *D, AccessSpecifier AS) {
54249261Sdim    Decls.push_back(DeclAccessPair::make(D, AS), C);
55249261Sdim  }
56249261Sdim
57249261Sdim  /// Replaces the given declaration with the new one, once.
58249261Sdim  ///
59249261Sdim  /// \return true if the set changed
60263509Sdim  bool replace(const NamedDecl *Old, NamedDecl *New, AccessSpecifier AS) {
61252723Sdim    for (DeclsTy::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
62252723Sdim      if (I->getDecl() == Old) {
63252723Sdim        I->set(New, AS);
64252723Sdim        return true;
65252723Sdim      }
66252723Sdim    }
67249261Sdim    return false;
68249261Sdim  }
69249261Sdim
70263509Sdim  void erase(unsigned I) { Decls[I] = Decls.pop_back_val(); }
71249261Sdim
72249261Sdim  void clear() { Decls.clear(); }
73249261Sdim
74249261Sdim  bool empty() const { return Decls.empty(); }
75249261Sdim  unsigned size() const { return Decls.size(); }
76249261Sdim
77249261Sdim  void reserve(ASTContext &C, unsigned N) {
78249261Sdim    Decls.reserve(C, N);
79249261Sdim  }
80249261Sdim
81249261Sdim  void append(ASTContext &C, iterator I, iterator E) {
82249261Sdim    Decls.append(C, I.ir, E.ir);
83249261Sdim  }
84249261Sdim
85249261Sdim  DeclAccessPair &operator[](unsigned I) { return Decls[I]; }
86249261Sdim  const DeclAccessPair &operator[](unsigned I) const { return Decls[I]; }
87249261Sdim};
88263509Sdim
89263509Sdim/// \brief An UnresolvedSet-like class that might not have been loaded from the
90263509Sdim/// external AST source yet.
91263509Sdimclass LazyASTUnresolvedSet {
92263509Sdim  mutable ASTUnresolvedSet Impl;
93263509Sdim
94263509Sdim  void getFromExternalSource(ASTContext &C) const;
95263509Sdim
96263509Sdimpublic:
97263509Sdim  ASTUnresolvedSet &get(ASTContext &C) const {
98263509Sdim    if (Impl.Decls.isLazy())
99263509Sdim      getFromExternalSource(C);
100263509Sdim    return Impl;
101263509Sdim  }
102263509Sdim
103263509Sdim  void reserve(ASTContext &C, unsigned N) { Impl.reserve(C, N); }
104263509Sdim  void addLazyDecl(ASTContext &C, uintptr_t ID, AccessSpecifier AS) {
105263509Sdim    assert(Impl.empty() || Impl.Decls.isLazy());
106263509Sdim    Impl.Decls.setLazy(true);
107263509Sdim    Impl.addDecl(C, reinterpret_cast<NamedDecl*>(ID << 2), AS);
108263509Sdim  }
109263509Sdim};
110263509Sdim
111249261Sdim} // namespace clang
112249261Sdim
113249261Sdim#endif
114