Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
Dmitri Gribenko | e4e77d0 | 2022-03-17 14:09:39 +0000 | [diff] [blame] | 5 | #ifndef CRUBIT_RS_BINDINGS_FROM_CC_IMPORTER_H_ |
| 6 | #define CRUBIT_RS_BINDINGS_FROM_CC_IMPORTER_H_ |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 7 | |
Marcel Hlopko | 7d73979 | 2021-08-12 07:52:47 +0000 | [diff] [blame] | 8 | #include <memory> |
Marcel Hlopko | 20f4ce4 | 2021-11-02 08:20:00 +0000 | [diff] [blame] | 9 | #include <optional> |
Lukasz Anforowicz | 49b5bbc | 2022-02-04 23:40:10 +0000 | [diff] [blame] | 10 | #include <set> |
Marcel Hlopko | f1123c8 | 2021-08-19 11:38:52 +0000 | [diff] [blame] | 11 | #include <string> |
Marcel Hlopko | 20f4ce4 | 2021-11-02 08:20:00 +0000 | [diff] [blame] | 12 | #include <utility> |
Marcel Hlopko | 20f4ce4 | 2021-11-02 08:20:00 +0000 | [diff] [blame] | 13 | #include <vector> |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 14 | |
Michael Forster | b3503e0 | 2022-04-25 00:24:14 -0700 | [diff] [blame] | 15 | #include "rs_bindings_from_cc/decl_importer.h" |
| 16 | #include "rs_bindings_from_cc/importers/class_template.h" |
Michael Forster | c0bc926 | 2022-04-25 00:30:26 -0700 | [diff] [blame] | 17 | #include "rs_bindings_from_cc/importers/cxx_record.h" |
Michael Forster | 4fa849d | 2022-04-25 00:32:59 -0700 | [diff] [blame] | 18 | #include "rs_bindings_from_cc/importers/enum.h" |
Michael Forster | 7e4244a | 2022-04-25 00:39:01 -0700 | [diff] [blame] | 19 | #include "rs_bindings_from_cc/importers/function.h" |
Michael Forster | 350d531 | 2022-04-25 00:36:31 -0700 | [diff] [blame] | 20 | #include "rs_bindings_from_cc/importers/function_template.h" |
Michael Forster | 284fb5a | 2022-04-25 00:41:14 -0700 | [diff] [blame] | 21 | #include "rs_bindings_from_cc/importers/namespace.h" |
Michael Forster | c60df1e | 2022-04-25 00:55:44 -0700 | [diff] [blame] | 22 | #include "rs_bindings_from_cc/importers/typedef_name.h" |
Marcel Hlopko | 3b254b3 | 2022-03-09 14:10:49 +0000 | [diff] [blame] | 23 | #include "rs_bindings_from_cc/ir.h" |
Lukasz Anforowicz | cec7a8a | 2022-04-27 10:24:51 -0700 | [diff] [blame] | 24 | #include "clang/AST/Mangle.h" |
Rosica Dejanovska | 2708a5f | 2022-06-22 06:54:59 -0700 | [diff] [blame] | 25 | #include "clang/AST/RawCommentList.h" |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 26 | |
Marcel Hlopko | f15e8ce | 2022-04-08 08:46:09 -0700 | [diff] [blame] | 27 | namespace crubit { |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 28 | |
Michael Forster | 64217b4 | 2022-04-22 05:48:54 -0700 | [diff] [blame] | 29 | // Iterates over the AST created from the invocation's entry headers and |
| 30 | // creates an intermediate representation of the import (`IR`) into the |
| 31 | // invocation object. |
| 32 | class Importer : public ImportContext { |
| 33 | public: |
| 34 | explicit Importer(Invocation& invocation, clang::ASTContext& ctx, |
| 35 | clang::Sema& sema) |
| 36 | : ImportContext(invocation, ctx, sema), |
| 37 | mangler_(CRUBIT_DIE_IF_NULL(ctx_.createMangleContext())) { |
| 38 | decl_importers_.push_back( |
| 39 | std::make_unique<ClassTemplateDeclImporter>(*this)); |
| 40 | decl_importers_.push_back(std::make_unique<CXXRecordDeclImporter>(*this)); |
| 41 | decl_importers_.push_back(std::make_unique<EnumDeclImporter>(*this)); |
| 42 | decl_importers_.push_back(std::make_unique<FunctionDeclImporter>(*this)); |
| 43 | decl_importers_.push_back( |
| 44 | std::make_unique<FunctionTemplateDeclImporter>(*this)); |
| 45 | decl_importers_.push_back(std::make_unique<NamespaceDeclImporter>(*this)); |
| 46 | decl_importers_.push_back(std::make_unique<TypedefNameDeclImporter>(*this)); |
| 47 | } |
| 48 | |
| 49 | // Import all visible declarations from a translation unit. |
| 50 | void Import(clang::TranslationUnitDecl* decl); |
| 51 | |
| 52 | protected: |
| 53 | // Implementation of `ImportContext` |
| 54 | void ImportDeclsFromDeclContext( |
| 55 | const clang::DeclContext* decl_context) override; |
| 56 | IR::Item ImportUnsupportedItem(const clang::Decl* decl, |
| 57 | std::string error) override; |
| 58 | IR::Item ImportUnsupportedItem(const clang::Decl* decl, |
| 59 | std::set<std::string> errors) override; |
| 60 | std::vector<ItemId> GetItemIdsInSourceOrder(clang::Decl* decl) override; |
| 61 | std::string GetMangledName(const clang::NamedDecl* named_decl) const override; |
| 62 | BazelLabel GetOwningTarget(const clang::Decl* decl) const override; |
| 63 | bool IsFromCurrentTarget(const clang::Decl* decl) const override; |
| 64 | std::optional<UnqualifiedIdentifier> GetTranslatedName( |
| 65 | const clang::NamedDecl* named_decl) const override; |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 66 | std::optional<Identifier> GetTranslatedIdentifier( |
Michael Forster | 64217b4 | 2022-04-22 05:48:54 -0700 | [diff] [blame] | 67 | const clang::NamedDecl* named_decl) const override { |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 68 | if (std::optional<UnqualifiedIdentifier> name = |
| 69 | GetTranslatedName(named_decl)) { |
| 70 | return std::move(*std::get_if<Identifier>(&*name)); |
| 71 | } |
| 72 | return std::nullopt; |
| 73 | } |
Michael Forster | 64217b4 | 2022-04-22 05:48:54 -0700 | [diff] [blame] | 74 | llvm::Optional<std::string> GetComment( |
| 75 | const clang::Decl* decl) const override; |
| 76 | SourceLoc ConvertSourceLocation(clang::SourceLocation loc) const override; |
Lukasz Anforowicz | 63559d3 | 2022-05-27 16:42:36 -0700 | [diff] [blame] | 77 | absl::StatusOr<MappedType> ConvertQualType( |
| 78 | clang::QualType qual_type, |
| 79 | std::optional<clang::tidy::lifetimes::ValueLifetimes>& lifetimes, |
Lukasz Anforowicz | 14732b2 | 2022-06-02 09:11:08 -0700 | [diff] [blame] | 80 | bool nullable = true) override; |
Lukasz Anforowicz | 0233f98 | 2022-05-27 16:38:20 -0700 | [diff] [blame] | 81 | void MarkAsSuccessfullyImported(const clang::TypeDecl* decl) override; |
Lukasz Anforowicz | 5258f76 | 2022-05-27 16:14:46 -0700 | [diff] [blame] | 82 | bool HasBeenAlreadySuccessfullyImported( |
| 83 | const clang::TypeDecl* decl) const override; |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 84 | |
Michael Forster | 64217b4 | 2022-04-22 05:48:54 -0700 | [diff] [blame] | 85 | private: |
Rosica Dejanovska | 2708a5f | 2022-06-22 06:54:59 -0700 | [diff] [blame] | 86 | class SourceOrderKey; |
| 87 | class SourceLocationComparator; |
| 88 | |
| 89 | // Returns a SourceOrderKey for the given `decl` that should be used for |
| 90 | // ordering Items. |
| 91 | SourceOrderKey GetSourceOrderKey(const clang::Decl* decl) const; |
| 92 | // Returns a SourceOrderKey for the given `comment` that should be used for |
| 93 | // ordering Items. |
| 94 | SourceOrderKey GetSourceOrderKey(const clang::RawComment* comment) const; |
| 95 | |
| 96 | // Returns a name for `decl` that should be used for ordering declarations. |
| 97 | std::string GetNameForSourceOrder(const clang::Decl* decl) const; |
| 98 | |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 99 | // Returns the item ids of template instantiations that have been triggered |
| 100 | // from the current target. The returned items are in an arbitrary, |
| 101 | // deterministic/reproducible order. |
| 102 | std::vector<ItemId> GetOrderedItemIdsOfTemplateInstantiations() const; |
| 103 | |
Michael Forster | 64217b4 | 2022-04-22 05:48:54 -0700 | [diff] [blame] | 104 | // Returns the Item of a Decl, importing it first if necessary. |
| 105 | std::optional<IR::Item> GetDeclItem(clang::Decl* decl); |
Googler | 4e1bc13 | 2021-12-06 10:10:42 +0000 | [diff] [blame] | 106 | |
Michael Forster | 64217b4 | 2022-04-22 05:48:54 -0700 | [diff] [blame] | 107 | // Imports a decl and creates an IR item (or error messages). |
| 108 | // Does not use or update the cache. |
| 109 | std::optional<IR::Item> ImportDecl(clang::Decl* decl); |
Marcel Hlopko | 3df7254 | 2021-11-30 09:38:36 +0000 | [diff] [blame] | 110 | |
Michael Forster | 64217b4 | 2022-04-22 05:48:54 -0700 | [diff] [blame] | 111 | // Stores the comments of this target in source order. |
| 112 | void ImportFreeComments(); |
Googler | 6c3de12 | 2022-03-28 11:40:41 +0000 | [diff] [blame] | 113 | |
Lukasz Anforowicz | 63559d3 | 2022-05-27 16:42:36 -0700 | [diff] [blame] | 114 | absl::StatusOr<MappedType> ConvertType( |
| 115 | const clang::Type* type, |
| 116 | std::optional<clang::tidy::lifetimes::ValueLifetimes>& lifetimes, |
Lukasz Anforowicz | 14732b2 | 2022-06-02 09:11:08 -0700 | [diff] [blame] | 117 | bool nullable); |
Lukasz Anforowicz | 63559d3 | 2022-05-27 16:42:36 -0700 | [diff] [blame] | 118 | absl::StatusOr<MappedType> ConvertTypeDecl(const clang::TypeDecl* decl) const; |
| 119 | |
Lukasz Anforowicz | 14732b2 | 2022-06-02 09:11:08 -0700 | [diff] [blame] | 120 | // Converts `type` into a MappedType, after first importing the Record behind |
| 121 | // the template instantiation. |
| 122 | absl::StatusOr<MappedType> ConvertTemplateSpecializationType( |
| 123 | const clang::TemplateSpecializationType* type); |
| 124 | |
Michael Forster | 64217b4 | 2022-04-22 05:48:54 -0700 | [diff] [blame] | 125 | std::vector<std::unique_ptr<DeclImporter>> decl_importers_; |
Marcel Hlopko | 7d73979 | 2021-08-12 07:52:47 +0000 | [diff] [blame] | 126 | std::unique_ptr<clang::MangleContext> mangler_; |
Lukasz Anforowicz | 06f76b1 | 2022-03-23 13:48:39 +0000 | [diff] [blame] | 127 | absl::flat_hash_map<const clang::Decl*, std::optional<IR::Item>> |
| 128 | import_cache_; |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 129 | absl::flat_hash_set<const clang::ClassTemplateSpecializationDecl*> |
| 130 | class_template_instantiations_for_current_target_; |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 131 | std::vector<const clang::RawComment*> comments_; |
Lukasz Anforowicz | 23fdfad | 2022-05-27 16:43:43 -0700 | [diff] [blame] | 132 | |
| 133 | // Set of decls that have been successfully imported (i.e. that will be |
| 134 | // present in the IR output / that will not produce dangling ItemIds in the IR |
| 135 | // output). |
| 136 | absl::flat_hash_set<const clang::TypeDecl*> known_type_decls_; |
Michael Forster | a49d2e6 | 2022-01-28 07:26:40 +0000 | [diff] [blame] | 137 | }; // class Importer |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 138 | |
Marcel Hlopko | f15e8ce | 2022-04-08 08:46:09 -0700 | [diff] [blame] | 139 | } // namespace crubit |
Marcel Hlopko | e8f1c4e | 2021-07-28 18:12:49 +0000 | [diff] [blame] | 140 | |
Dmitri Gribenko | e4e77d0 | 2022-03-17 14:09:39 +0000 | [diff] [blame] | 141 | #endif // CRUBIT_RS_BINDINGS_FROM_CC_IMPORTER_H_ |