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