xref: /llvm-project/clang/include/clang/AST/ASTImporterSharedState.h (revision 6ad0788c332bb2043142954d300c49ac3e537f34)
12afbfb6bSGabor Marton //===- ASTImporterSharedState.h - ASTImporter specific state --*- C++ -*---===//
22afbfb6bSGabor Marton //
3c874dd53SChristopher Di Bella // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c874dd53SChristopher Di Bella // See https://llvm.org/LICENSE.txt for license information.
5c874dd53SChristopher Di Bella // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62afbfb6bSGabor Marton //
72afbfb6bSGabor Marton //===----------------------------------------------------------------------===//
82afbfb6bSGabor Marton //
92afbfb6bSGabor Marton //  This file defines the ASTImporter specific state, which may be shared
102afbfb6bSGabor Marton //  amongst several ASTImporter objects.
112afbfb6bSGabor Marton //
122afbfb6bSGabor Marton //===----------------------------------------------------------------------===//
132afbfb6bSGabor Marton 
142afbfb6bSGabor Marton #ifndef LLVM_CLANG_AST_ASTIMPORTERSHAREDSTATE_H
152afbfb6bSGabor Marton #define LLVM_CLANG_AST_ASTIMPORTERSHAREDSTATE_H
162afbfb6bSGabor Marton 
17dcb90675SphyBrackets #include "clang/AST/ASTImportError.h"
182afbfb6bSGabor Marton #include "clang/AST/ASTImporterLookupTable.h"
198758dce4SBenjamin Kramer #include "clang/AST/Decl.h"
202afbfb6bSGabor Marton #include "llvm/ADT/DenseMap.h"
21a1580d7bSKazu Hirata #include <optional>
222afbfb6bSGabor Marton 
232afbfb6bSGabor Marton namespace clang {
242afbfb6bSGabor Marton 
252afbfb6bSGabor Marton class TranslationUnitDecl;
262afbfb6bSGabor Marton 
272afbfb6bSGabor Marton /// Importer specific state, which may be shared amongst several ASTImporter
282afbfb6bSGabor Marton /// objects.
292afbfb6bSGabor Marton class ASTImporterSharedState {
302afbfb6bSGabor Marton 
312afbfb6bSGabor Marton   /// Pointer to the import specific lookup table.
322afbfb6bSGabor Marton   std::unique_ptr<ASTImporterLookupTable> LookupTable;
332afbfb6bSGabor Marton 
342afbfb6bSGabor Marton   /// Mapping from the already-imported declarations in the "to"
352afbfb6bSGabor Marton   /// context to the error status of the import of that declaration.
362afbfb6bSGabor Marton   /// This map contains only the declarations that were not correctly
372afbfb6bSGabor Marton   /// imported. The same declaration may or may not be included in
382afbfb6bSGabor Marton   /// ImportedFromDecls. This map is updated continuously during imports and
392afbfb6bSGabor Marton   /// never cleared (like ImportedFromDecls).
409d637956SphyBrackets   llvm::DenseMap<Decl *, ASTImportError> ImportErrors;
412afbfb6bSGabor Marton 
4225ac078aSGabor Marton   /// Set of the newly created declarations.
4325ac078aSGabor Marton   llvm::DenseSet<Decl *> NewDecls;
4425ac078aSGabor Marton 
452afbfb6bSGabor Marton   // FIXME put ImportedFromDecls here!
462afbfb6bSGabor Marton   // And from that point we can better encapsulate the lookup table.
472afbfb6bSGabor Marton 
482afbfb6bSGabor Marton public:
492afbfb6bSGabor Marton   ASTImporterSharedState() = default;
502afbfb6bSGabor Marton 
ASTImporterSharedState(TranslationUnitDecl & ToTU)512afbfb6bSGabor Marton   ASTImporterSharedState(TranslationUnitDecl &ToTU) {
522b3d49b6SJonas Devlieghere     LookupTable = std::make_unique<ASTImporterLookupTable>(ToTU);
532afbfb6bSGabor Marton   }
542afbfb6bSGabor Marton 
getLookupTable()552afbfb6bSGabor Marton   ASTImporterLookupTable *getLookupTable() { return LookupTable.get(); }
562afbfb6bSGabor Marton 
addDeclToLookup(Decl * D)572afbfb6bSGabor Marton   void addDeclToLookup(Decl *D) {
582afbfb6bSGabor Marton     if (LookupTable)
592afbfb6bSGabor Marton       if (auto *ND = dyn_cast<NamedDecl>(D))
602afbfb6bSGabor Marton         LookupTable->add(ND);
612afbfb6bSGabor Marton   }
622afbfb6bSGabor Marton 
removeDeclFromLookup(Decl * D)632afbfb6bSGabor Marton   void removeDeclFromLookup(Decl *D) {
642afbfb6bSGabor Marton     if (LookupTable)
652afbfb6bSGabor Marton       if (auto *ND = dyn_cast<NamedDecl>(D))
662afbfb6bSGabor Marton         LookupTable->remove(ND);
672afbfb6bSGabor Marton   }
682afbfb6bSGabor Marton 
getImportDeclErrorIfAny(Decl * ToD)69*6ad0788cSKazu Hirata   std::optional<ASTImportError> getImportDeclErrorIfAny(Decl *ToD) const {
702afbfb6bSGabor Marton     auto Pos = ImportErrors.find(ToD);
712afbfb6bSGabor Marton     if (Pos != ImportErrors.end())
722afbfb6bSGabor Marton       return Pos->second;
732afbfb6bSGabor Marton     else
74e31564afSKazu Hirata       return std::nullopt;
752afbfb6bSGabor Marton   }
762afbfb6bSGabor Marton 
setImportDeclError(Decl * To,ASTImportError Error)779d637956SphyBrackets   void setImportDeclError(Decl *To, ASTImportError Error) {
782afbfb6bSGabor Marton     ImportErrors[To] = Error;
792afbfb6bSGabor Marton   }
8025ac078aSGabor Marton 
isNewDecl(const Decl * ToD)8125ac078aSGabor Marton   bool isNewDecl(const Decl *ToD) const { return NewDecls.count(ToD); }
8225ac078aSGabor Marton 
markAsNewDecl(Decl * ToD)8325ac078aSGabor Marton   void markAsNewDecl(Decl *ToD) { NewDecls.insert(ToD); }
842afbfb6bSGabor Marton };
852afbfb6bSGabor Marton 
862afbfb6bSGabor Marton } // namespace clang
872afbfb6bSGabor Marton #endif // LLVM_CLANG_AST_ASTIMPORTERSHAREDSTATE_H
88