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 
22 namespace clang {
23 
24 class TranslationUnitDecl;
25 
26 /// Importer specific state, which may be shared amongst several ASTImporter
27 /// objects.
28 class ASTImporterSharedState {
29 
30   /// Pointer to the import specific lookup table.
31   std::unique_ptr<ASTImporterLookupTable> LookupTable;
32 
33   /// Mapping from the already-imported declarations in the "to"
34   /// context to the error status of the import of that declaration.
35   /// This map contains only the declarations that were not correctly
36   /// imported. The same declaration may or may not be included in
37   /// ImportedFromDecls. This map is updated continuously during imports and
38   /// never cleared (like ImportedFromDecls).
39   llvm::DenseMap<Decl *, ASTImportError> ImportErrors;
40 
41   /// Set of the newly created declarations.
42   llvm::DenseSet<Decl *> NewDecls;
43 
44   // FIXME put ImportedFromDecls here!
45   // And from that point we can better encapsulate the lookup table.
46 
47 public:
48   ASTImporterSharedState() = default;
49 
50   ASTImporterSharedState(TranslationUnitDecl &ToTU) {
51     LookupTable = std::make_unique<ASTImporterLookupTable>(ToTU);
52   }
53 
54   ASTImporterLookupTable *getLookupTable() { return LookupTable.get(); }
55 
56   void addDeclToLookup(Decl *D) {
57     if (LookupTable)
58       if (auto *ND = dyn_cast<NamedDecl>(D))
59         LookupTable->add(ND);
60   }
61 
62   void removeDeclFromLookup(Decl *D) {
63     if (LookupTable)
64       if (auto *ND = dyn_cast<NamedDecl>(D))
65         LookupTable->remove(ND);
66   }
67 
68   llvm::Optional<ASTImportError> getImportDeclErrorIfAny(Decl *ToD) const {
69     auto Pos = ImportErrors.find(ToD);
70     if (Pos != ImportErrors.end())
71       return Pos->second;
72     else
73       return Optional<ASTImportError>();
74   }
75 
76   void setImportDeclError(Decl *To, ASTImportError Error) {
77     ImportErrors[To] = Error;
78   }
79 
80   bool isNewDecl(const Decl *ToD) const { return NewDecls.count(ToD); }
81 
82   void markAsNewDecl(Decl *ToD) { NewDecls.insert(ToD); }
83 };
84 
85 } // namespace clang
86 #endif // LLVM_CLANG_AST_ASTIMPORTERSHAREDSTATE_H
87