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 { 25263508Sdim struct DeclsTy : ASTVector<DeclAccessPair> { 26263508Sdim DeclsTy() {} 27263508Sdim DeclsTy(ASTContext &C, unsigned N) : ASTVector<DeclAccessPair>(C, N) {} 28263508Sdim 29263508Sdim bool isLazy() const { return getTag(); } 30263508Sdim void setLazy(bool Lazy) { setTag(Lazy); } 31263508Sdim }; 32263508Sdim 33249261Sdim DeclsTy Decls; 34249261Sdim 35249261Sdim ASTUnresolvedSet(const ASTUnresolvedSet &) LLVM_DELETED_FUNCTION; 36249261Sdim void operator=(const ASTUnresolvedSet &) LLVM_DELETED_FUNCTION; 37249261Sdim 38263508Sdim friend class LazyASTUnresolvedSet; 39263508Sdim 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 60263508Sdim bool replace(const NamedDecl *Old, NamedDecl *New, AccessSpecifier AS) { 61251662Sdim for (DeclsTy::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) { 62251662Sdim if (I->getDecl() == Old) { 63251662Sdim I->set(New, AS); 64251662Sdim return true; 65251662Sdim } 66251662Sdim } 67249261Sdim return false; 68249261Sdim } 69249261Sdim 70263508Sdim 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}; 88263508Sdim 89263508Sdim/// \brief An UnresolvedSet-like class that might not have been loaded from the 90263508Sdim/// external AST source yet. 91263508Sdimclass LazyASTUnresolvedSet { 92263508Sdim mutable ASTUnresolvedSet Impl; 93263508Sdim 94263508Sdim void getFromExternalSource(ASTContext &C) const; 95263508Sdim 96263508Sdimpublic: 97263508Sdim ASTUnresolvedSet &get(ASTContext &C) const { 98263508Sdim if (Impl.Decls.isLazy()) 99263508Sdim getFromExternalSource(C); 100263508Sdim return Impl; 101263508Sdim } 102263508Sdim 103263508Sdim void reserve(ASTContext &C, unsigned N) { Impl.reserve(C, N); } 104263508Sdim void addLazyDecl(ASTContext &C, uintptr_t ID, AccessSpecifier AS) { 105263508Sdim assert(Impl.empty() || Impl.Decls.isLazy()); 106263508Sdim Impl.Decls.setLazy(true); 107263508Sdim Impl.addDecl(C, reinterpret_cast<NamedDecl*>(ID << 2), AS); 108263508Sdim } 109263508Sdim}; 110263508Sdim 111249261Sdim} // namespace clang 112249261Sdim 113249261Sdim#endif 114