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