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