xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/CxxModuleHandler.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
15ffd83dbSDimitry Andric //===-- CxxModuleHandler.h --------------------------------------*- C++ -*-===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric 
95ffd83dbSDimitry Andric #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CXXMODULEHANDLER_H
105ffd83dbSDimitry Andric #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CXXMODULEHANDLER_H
115ffd83dbSDimitry Andric 
125ffd83dbSDimitry Andric #include "clang/AST/ASTImporter.h"
135ffd83dbSDimitry Andric #include "clang/Sema/Sema.h"
145ffd83dbSDimitry Andric #include "llvm/ADT/StringSet.h"
15*bdd1243dSDimitry Andric #include <optional>
165ffd83dbSDimitry Andric 
175ffd83dbSDimitry Andric namespace lldb_private {
185ffd83dbSDimitry Andric 
195ffd83dbSDimitry Andric /// Handles importing decls into an ASTContext with an attached C++ module.
205ffd83dbSDimitry Andric ///
215ffd83dbSDimitry Andric /// This class searches a C++ module (which must be attached to the target
225ffd83dbSDimitry Andric /// ASTContext) for an equivalent decl to the one that should be imported.
235ffd83dbSDimitry Andric /// If the decl that is found in the module is a suitable replacement
245ffd83dbSDimitry Andric /// for the decl that should be imported, the module decl will be treated as
255ffd83dbSDimitry Andric /// the result of the import process.
265ffd83dbSDimitry Andric ///
275ffd83dbSDimitry Andric /// If the Decl that should be imported is a template specialization
285ffd83dbSDimitry Andric /// that doesn't exist yet in the target ASTContext (e.g. `std::vector<int>`),
295ffd83dbSDimitry Andric /// then this class tries to create the template specialization in the target
305ffd83dbSDimitry Andric /// ASTContext. This is only possible if the CxxModuleHandler can determine
315ffd83dbSDimitry Andric /// that instantiating this template is safe to do, e.g. because the target
325ffd83dbSDimitry Andric /// decl is a container class from the STL.
335ffd83dbSDimitry Andric class CxxModuleHandler {
345ffd83dbSDimitry Andric   /// The ASTImporter that should be used to import any Decls which aren't
355ffd83dbSDimitry Andric   /// directly handled by this class itself.
365ffd83dbSDimitry Andric   clang::ASTImporter *m_importer = nullptr;
375ffd83dbSDimitry Andric 
385ffd83dbSDimitry Andric   /// The Sema instance of the target ASTContext.
395ffd83dbSDimitry Andric   clang::Sema *m_sema = nullptr;
405ffd83dbSDimitry Andric 
415ffd83dbSDimitry Andric   /// List of template names this class currently supports. These are the
425ffd83dbSDimitry Andric   /// template names inside the 'std' namespace such as 'vector' or 'list'.
435ffd83dbSDimitry Andric   llvm::StringSet<> m_supported_templates;
445ffd83dbSDimitry Andric 
455ffd83dbSDimitry Andric   /// Tries to manually instantiate the given foreign template in the target
465ffd83dbSDimitry Andric   /// context (designated by m_sema).
47*bdd1243dSDimitry Andric   std::optional<clang::Decl *> tryInstantiateStdTemplate(clang::Decl *d);
485ffd83dbSDimitry Andric 
495ffd83dbSDimitry Andric public:
505ffd83dbSDimitry Andric   CxxModuleHandler() = default;
515ffd83dbSDimitry Andric   CxxModuleHandler(clang::ASTImporter &importer, clang::ASTContext *target);
525ffd83dbSDimitry Andric 
535ffd83dbSDimitry Andric   /// Attempts to import the given decl into the target ASTContext by
545ffd83dbSDimitry Andric   /// deserializing it from the 'std' module. This function returns a Decl if a
555ffd83dbSDimitry Andric   /// Decl has been deserialized from the 'std' module. Otherwise this function
565ffd83dbSDimitry Andric   /// returns nothing.
57*bdd1243dSDimitry Andric   std::optional<clang::Decl *> Import(clang::Decl *d);
585ffd83dbSDimitry Andric 
595ffd83dbSDimitry Andric   /// Returns true iff this instance is capable of importing any declarations
605ffd83dbSDimitry Andric   /// in the target ASTContext.
isValid()615ffd83dbSDimitry Andric   bool isValid() const { return m_sema != nullptr; }
625ffd83dbSDimitry Andric };
635ffd83dbSDimitry Andric 
645ffd83dbSDimitry Andric } // namespace lldb_private
655ffd83dbSDimitry Andric 
665ffd83dbSDimitry Andric #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CXXMODULEHANDLER_H
67