blob: 28163af9aa893f1d429cf1cee3eca8c6fbce8360 [file] [log] [blame]
Brian Silverman4e662aa2022-05-11 23:10:19 -07001// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use proc_macro2::TokenStream;
10use quote::{ToTokens, TokenStreamExt};
11use syn::ItemMod;
12
13use crate::{do_cxx_cpp_generation, parse_file::CppBuildable, CppCodegenOptions, GeneratedCpp};
14
15/// A struct to represent a cxx::bridge (i.e. some manual bindings)
16/// found in a file. autocxx knows about them so that we can generate C++
17/// for both manual and automatic bindings using the same tooling.
18pub struct CxxBridge {
19 tokens: TokenStream,
20}
21
22impl From<ItemMod> for CxxBridge {
23 fn from(itm: ItemMod) -> Self {
24 Self {
25 tokens: itm.to_token_stream(),
26 }
27 }
28}
29
30impl ToTokens for CxxBridge {
31 fn to_tokens(&self, tokens: &mut TokenStream) {
32 tokens.append_all(self.tokens.clone());
33 }
34}
35
36impl CppBuildable for CxxBridge {
37 fn generate_h_and_cxx(
38 &self,
39 cpp_codegen_options: &CppCodegenOptions,
40 ) -> Result<GeneratedCpp, cxx_gen::Error> {
41 let header_name = cpp_codegen_options.cxxgen_header_namer.name_header();
42 let fp = do_cxx_cpp_generation(self.tokens.clone(), cpp_codegen_options, header_name)?;
43 Ok(GeneratedCpp(vec![fp]))
44 }
45}