1 //===- ASTImporterSharedState.h - ASTImporter specific state --*- C++ -*---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file defines the ASTImporter specific state, which may be shared
10 //  amongst several ASTImporter objects.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_AST_ASTIMPORTERSHAREDSTATE_H
15 #define LLVM_CLANG_AST_ASTIMPORTERSHAREDSTATE_H
16 
17 #include "clang/AST/ASTImportError.h"
18 #include "clang/AST/ASTImporterLookupTable.h"
19 #include "clang/AST/Decl.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include <optional>
22 
23 namespace clang {
24 
25 class TranslationUnitDecl;
26 
27 /// Importer specific state, which may be shared amongst several ASTImporter
28 /// objects.
29 class ASTImporterSharedState {
30 
31   /// Pointer to the import specific lookup table.
32   std::unique_ptr<ASTImporterLookupTable> LookupTable;
33 
34   /// Mapping from the already-imported declarations in the "to"
35   /// context to the error status of the import of that declaration.
36   /// This map contains only the declarations that were not correctly
37   /// imported. The same declaration may or may not be included in
38   /// ImportedFromDecls. This map is updated continuously during imports and
39   /// never cleared (like ImportedFromDecls).
40   llvm::DenseMap<Decl *, ASTImportError> ImportErrors;
41 
42   /// Set of the newly created declarations.
43   llvm::DenseSet<Decl *> NewDecls;
44 
45   // FIXME put ImportedFromDecls here!
46   // And from that point we can better encapsulate the lookup table.
47 
48 public:
49   ASTImporterSharedState() = default;
50 
ASTImporterSharedState(TranslationUnitDecl & ToTU)51   ASTImporterSharedState(TranslationUnitDecl &ToTU) {
52     LookupTable = std::make_unique<ASTImporterLookupTable>(ToTU);
53   }
54 
getLookupTable()55   ASTImporterLookupTable *getLookupTable() { return LookupTable.get(); }
56 
addDeclToLookup(Decl * D)57   void addDeclToLookup(Decl *D) {
58     if (LookupTable)
59       if (auto *ND = dyn_cast<NamedDecl>(D))
60         LookupTable->add(ND);
61   }
62 
removeDeclFromLookup(Decl * D)63   void removeDeclFromLookup(Decl *D) {
64     if (LookupTable)
65       if (auto *ND = dyn_cast<NamedDecl>(D))
66         LookupTable->remove(ND);
67   }
68 
getImportDeclErrorIfAny(Decl * ToD)69   std::optional<ASTImportError> getImportDeclErrorIfAny(Decl *ToD) const {
70     auto Pos = ImportErrors.find(ToD);
71     if (Pos != ImportErrors.end())
72       return Pos->second;
73     else
74       return std::nullopt;
75   }
76 
setImportDeclError(Decl * To,ASTImportError Error)77   void setImportDeclError(Decl *To, ASTImportError Error) {
78     ImportErrors[To] = Error;
79   }
80 
isNewDecl(const Decl * ToD)81   bool isNewDecl(const Decl *ToD) const { return NewDecls.count(ToD); }
82 
markAsNewDecl(Decl * ToD)83   void markAsNewDecl(Decl *ToD) { NewDecls.insert(ToD); }
84 };
85 
86 } // namespace clang
87 #endif // LLVM_CLANG_AST_ASTIMPORTERSHAREDSTATE_H
88