1 //===-- CxxModuleHandler.h --------------------------------------*- 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 #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CXXMODULEHANDLER_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CXXMODULEHANDLER_H
11 
12 #include "clang/AST/ASTImporter.h"
13 #include "clang/Sema/Sema.h"
14 #include "llvm/ADT/StringSet.h"
15 
16 namespace lldb_private {
17 
18 /// Handles importing decls into an ASTContext with an attached C++ module.
19 ///
20 /// This class searches a C++ module (which must be attached to the target
21 /// ASTContext) for an equivalent decl to the one that should be imported.
22 /// If the decl that is found in the module is a suitable replacement
23 /// for the decl that should be imported, the module decl will be treated as
24 /// the result of the import process.
25 ///
26 /// If the Decl that should be imported is a template specialization
27 /// that doesn't exist yet in the target ASTContext (e.g. `std::vector<int>`),
28 /// then this class tries to create the template specialization in the target
29 /// ASTContext. This is only possible if the CxxModuleHandler can determine
30 /// that instantiating this template is safe to do, e.g. because the target
31 /// decl is a container class from the STL.
32 class CxxModuleHandler {
33   /// The ASTImporter that should be used to import any Decls which aren't
34   /// directly handled by this class itself.
35   clang::ASTImporter *m_importer = nullptr;
36 
37   /// The Sema instance of the target ASTContext.
38   clang::Sema *m_sema = nullptr;
39 
40   /// List of template names this class currently supports. These are the
41   /// template names inside the 'std' namespace such as 'vector' or 'list'.
42   llvm::StringSet<> m_supported_templates;
43 
44   /// Tries to manually instantiate the given foreign template in the target
45   /// context (designated by m_sema).
46   llvm::Optional<clang::Decl *> tryInstantiateStdTemplate(clang::Decl *d);
47 
48 public:
49   CxxModuleHandler() = default;
50   CxxModuleHandler(clang::ASTImporter &importer, clang::ASTContext *target);
51 
52   /// Attempts to import the given decl into the target ASTContext by
53   /// deserializing it from the 'std' module. This function returns a Decl if a
54   /// Decl has been deserialized from the 'std' module. Otherwise this function
55   /// returns nothing.
56   llvm::Optional<clang::Decl *> Import(clang::Decl *d);
57 
58   /// Returns true iff this instance is capable of importing any declarations
59   /// in the target ASTContext.
60   bool isValid() const { return m_sema != nullptr; }
61 };
62 
63 } // namespace lldb_private
64 
65 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CXXMODULEHANDLER_H
66