blob: 61a53f53f0fe4d698280d2dabecb9842be56e8b2 [file] [log] [blame]
Googler741ed9c2021-10-01 08:00:49 +00001// 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 Gribenkoe4e77d02022-03-17 14:09:39 +00005#ifndef CRUBIT_RS_BINDINGS_FROM_CC_IR_FROM_CC_H_
6#define CRUBIT_RS_BINDINGS_FROM_CC_IR_FROM_CC_H_
Googler741ed9c2021-10-01 08:00:49 +00007
Marcel Hlopko7aa38a72021-11-11 07:39:51 +00008#include <string>
Devin Jeanpierreb0af4dc2023-04-27 06:59:25 -07009#include <type_traits>
Marcel Hlopko7aa38a72021-11-11 07:39:51 +000010
Lukasz Anforowiczcec7a8a2022-04-27 10:24:51 -070011#include "absl/container/flat_hash_map.h"
Devin Jeanpierre6ed0f602023-03-01 17:22:54 -080012#include "absl/container/flat_hash_set.h"
Lukasz Anforowiczcec7a8a2022-04-27 10:24:51 -070013#include "absl/status/statusor.h"
14#include "absl/strings/string_view.h"
15#include "absl/types/span.h"
Marcel Hlopko3b254b32022-03-09 14:10:49 +000016#include "rs_bindings_from_cc/bazel_types.h"
17#include "rs_bindings_from_cc/ir.h"
Googler741ed9c2021-10-01 08:00:49 +000018
Marcel Hlopkof15e8ce2022-04-08 08:46:09 -070019namespace crubit {
Googler741ed9c2021-10-01 08:00:49 +000020
Marcel Hlopko20de6de2022-09-13 05:28:08 -070021// Name of the namespace in which we generate code that triggers class template
22// instantiations.
23static constexpr absl::string_view kInstantiationsNamespaceName =
24 "__cc_template_instantiations";
25
Devin Jeanpierreb0af4dc2023-04-27 06:59:25 -070026struct NonCopyable final {
27 NonCopyable() = default;
28 NonCopyable(const NonCopyable&) = delete;
29};
30
31struct IrFromCcOptions final {
32 absl::string_view extra_source_code_for_testing = "";
33 BazelLabel current_target = BazelLabel{"//test:testing_target"};
34 absl::Span<const HeaderName> public_headers = {};
35 absl::flat_hash_map<const HeaderName, const std::string>
36 virtual_headers_contents_for_testing = {};
37 absl::flat_hash_map<HeaderName, BazelLabel> headers_to_targets = {};
38 absl::Span<const std::string> extra_rs_srcs = {};
39 absl::Span<const absl::string_view> clang_args = {};
40 absl::Span<const std::string> extra_instantiations = {};
41 absl::flat_hash_map<BazelLabel, absl::flat_hash_set<std::string>>
42 crubit_features = {};
43
44 // Not an argument, just here to prevent the options struct from being
45 // copied/moved with nontrivial lifetime implications.
46 NonCopyable do_not_copy = {};
47};
48
49static_assert(std::is_aggregate_v<IrFromCcOptions>);
50
Devin Jeanpierredf4dc8b2021-10-21 12:53:19 +000051// Parses C++ source code into IR.
52//
53// Parameters:
Lukasz Anforowicz121338a2022-11-01 14:28:32 -070054// * `extra_source_code_for_testing`: C++ source code to be written into a
55// virtual header and included in binding generation. Intended for testing
56// only.
Marcel Hlopko7aa38a72021-11-11 07:39:51 +000057// * `current_target`: full label of the target for which we generate bindings.
58// If not specified `//test:testing_target` is used.
59// * `public_headers`: names of headers from which we build the Clang AST. If
Lukasz Anforowicz121338a2022-11-01 14:28:32 -070060// `extra_source_code_for_testing` is specified its header will be added
61// automatically.
62// * `virtual_headers_contents_for_testing`: names and contents of virtual
63// headers that will be created in the virtual filesystem. These headers have
64// to be manually added to `public_headers` if needed.
Marcel Hlopko7aa38a72021-11-11 07:39:51 +000065// * `headers_to_targets`: mapping of headers to the label of the owning target.
66// If `extra_source_code` is specified it's added automatically under
Lukasz Anforowicz121338a2022-11-01 14:28:32 -070067// `//test:testing_target`. Headers from
68// `virtual_headers_contents_for_testing` are not added automatically.
69// * `clang_args`: additional command line arguments for Clang
Devin Jeanpierre96bf0bd2022-10-04 20:32:15 -070070// * `extra_rs_srcs`: A list of paths for additional rust files to include into
Devin Jeanpierre6ed0f602023-03-01 17:22:54 -080071// the crate. This is done via `#[path="..."] mod <...>; pub use <...>::*;`.
Marcel Hlopko2ee23912022-05-09 06:13:55 -070072// * `extra_instantiations`: names of full C++ class template specializations
Devin Jeanpierre6ed0f602023-03-01 17:22:54 -080073// to instantiate and generate bindings from.
74// * `crubit_features`: The set of Crubit features to enable for each target.
Devin Jeanpierredf4dc8b2021-10-21 12:53:19 +000075//
Devin Jeanpierreb0af4dc2023-04-27 06:59:25 -070076absl::StatusOr<IR> IrFromCc(IrFromCcOptions options);
Googler741ed9c2021-10-01 08:00:49 +000077
Marcel Hlopkof15e8ce2022-04-08 08:46:09 -070078} // namespace crubit
Googler741ed9c2021-10-01 08:00:49 +000079
Dmitri Gribenkoe4e77d02022-03-17 14:09:39 +000080#endif // CRUBIT_RS_BINDINGS_FROM_CC_IR_FROM_CC_H_