ASTUnresolvedSet.h revision c2d775714f79af977672e4f1dbc16ee9e02d1dea
1//===-- ASTUnresolvedSet.h - Unresolved sets of declarations  ---*- 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 provides an UnresolvedSet-like class, whose contents are
11//  allocated using the allocator associated with an ASTContext.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_AST_ASTUNRESOLVEDSET_H
16#define LLVM_CLANG_AST_ASTUNRESOLVEDSET_H
17
18#include "clang/AST/ASTVector.h"
19#include "clang/AST/UnresolvedSet.h"
20
21namespace clang {
22
23/// \brief An UnresolvedSet-like class which uses the ASTContext's allocator.
24class ASTUnresolvedSet {
25  struct DeclsTy : ASTVector<DeclAccessPair> {
26    DeclsTy() {}
27    DeclsTy(ASTContext &C, unsigned N) : ASTVector<DeclAccessPair>(C, N) {}
28
29    bool isLazy() const { return getTag(); }
30    void setLazy(bool Lazy) { setTag(Lazy); }
31  };
32
33  DeclsTy Decls;
34
35  ASTUnresolvedSet(const ASTUnresolvedSet &) LLVM_DELETED_FUNCTION;
36  void operator=(const ASTUnresolvedSet &) LLVM_DELETED_FUNCTION;
37
38  friend class LazyASTUnresolvedSet;
39
40public:
41  ASTUnresolvedSet() {}
42  ASTUnresolvedSet(ASTContext &C, unsigned N) : Decls(C, N) {}
43
44  typedef UnresolvedSetIterator iterator;
45  typedef UnresolvedSetIterator const_iterator;
46
47  iterator begin() { return iterator(Decls.begin()); }
48  iterator end() { return iterator(Decls.end()); }
49
50  const_iterator begin() const { return const_iterator(Decls.begin()); }
51  const_iterator end() const { return const_iterator(Decls.end()); }
52
53  void addDecl(ASTContext &C, NamedDecl *D, AccessSpecifier AS) {
54    Decls.push_back(DeclAccessPair::make(D, AS), C);
55  }
56
57  /// Replaces the given declaration with the new one, once.
58  ///
59  /// \return true if the set changed
60  bool replace(const NamedDecl *Old, NamedDecl *New, AccessSpecifier AS) {
61    for (DeclsTy::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) {
62      if (I->getDecl() == Old) {
63        I->set(New, AS);
64        return true;
65      }
66    }
67    return false;
68  }
69
70  void erase(unsigned I) { Decls[I] = Decls.pop_back_val(); }
71
72  void clear() { Decls.clear(); }
73
74  bool empty() const { return Decls.empty(); }
75  unsigned size() const { return Decls.size(); }
76
77  void reserve(ASTContext &C, unsigned N) {
78    Decls.reserve(C, N);
79  }
80
81  void append(ASTContext &C, iterator I, iterator E) {
82    Decls.append(C, I.ir, E.ir);
83  }
84
85  DeclAccessPair &operator[](unsigned I) { return Decls[I]; }
86  const DeclAccessPair &operator[](unsigned I) const { return Decls[I]; }
87};
88
89/// \brief An UnresolvedSet-like class that might not have been loaded from the
90/// external AST source yet.
91class LazyASTUnresolvedSet {
92  mutable ASTUnresolvedSet Impl;
93
94  void getFromExternalSource(ASTContext &C) const;
95
96public:
97  ASTUnresolvedSet &get(ASTContext &C) const {
98    if (Impl.Decls.isLazy())
99      getFromExternalSource(C);
100    return Impl;
101  }
102
103  void reserve(ASTContext &C, unsigned N) { Impl.reserve(C, N); }
104  void addLazyDecl(ASTContext &C, uintptr_t ID, AccessSpecifier AS) {
105    assert(Impl.empty() || Impl.Decls.isLazy());
106    Impl.Decls.setLazy(true);
107    Impl.addDecl(C, reinterpret_cast<NamedDecl*>(ID << 2), AS);
108  }
109};
110
111} // namespace clang
112
113#endif
114