blob: c7b3c3c3aa49c5b940a3abbfa2fa8b9e8e53adfb [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001// Functionality that is shared between the cxx::generate_bridge entry point and
2// the cmd.
3
4mod error;
5pub(super) mod include;
6pub(super) mod out;
7mod write;
8
David Tolnay52509842020-04-25 19:38:59 -07009use self::error::{format_err, Error, Result};
David Tolnay08419302020-04-19 20:38:20 -070010use crate::syntax::namespace::Namespace;
David Tolnayb6cf3142020-04-19 20:56:09 -070011use crate::syntax::{self, check, Types};
David Tolnay7db73692019-10-20 14:51:12 -040012use quote::quote;
13use std::fs;
David Tolnay7db73692019-10-20 14:51:12 -040014use std::path::Path;
David Tolnayb6cf3142020-04-19 20:56:09 -070015use syn::{Attribute, File, Item};
David Tolnay7db73692019-10-20 14:51:12 -040016
17struct Input {
David Tolnay754e21c2020-03-29 20:58:46 -070018 namespace: Namespace,
David Tolnay7db73692019-10-20 14:51:12 -040019 module: Vec<Item>,
20}
21
David Tolnay33d30292020-03-18 18:02:02 -070022#[derive(Default)]
23pub(super) struct Opt {
24 /// Any additional headers to #include
25 pub include: Vec<String>,
26}
27
David Tolnay7ece56f2020-03-29 21:21:38 -070028pub(super) fn do_generate_bridge(path: &Path, opt: Opt) -> Vec<u8> {
David Tolnay7db73692019-10-20 14:51:12 -040029 let header = false;
David Tolnay33d30292020-03-18 18:02:02 -070030 generate(path, opt, header)
David Tolnay7db73692019-10-20 14:51:12 -040031}
32
David Tolnay7ece56f2020-03-29 21:21:38 -070033pub(super) fn do_generate_header(path: &Path, opt: Opt) -> Vec<u8> {
David Tolnay7db73692019-10-20 14:51:12 -040034 let header = true;
David Tolnay33d30292020-03-18 18:02:02 -070035 generate(path, opt, header)
David Tolnay7db73692019-10-20 14:51:12 -040036}
37
David Tolnay7ece56f2020-03-29 21:21:38 -070038fn generate(path: &Path, opt: Opt, header: bool) -> Vec<u8> {
David Tolnay7db73692019-10-20 14:51:12 -040039 let source = match fs::read_to_string(path) {
40 Ok(source) => source,
41 Err(err) => format_err(path, "", Error::Io(err)),
42 };
43 match (|| -> Result<_> {
David Tolnay761a5fc2020-05-01 16:02:02 -070044 proc_macro2::fallback::force();
David Tolnay7db73692019-10-20 14:51:12 -040045 let syntax = syn::parse_file(&source)?;
46 let bridge = find_bridge_mod(syntax)?;
David Tolnay2ec14632020-05-04 00:47:10 -070047 let ref namespace = bridge.namespace;
48 let ref apis = syntax::parse_items(bridge.module)?;
49 let ref types = Types::collect(apis)?;
50 check::typecheck(namespace, apis, types)?;
51 let out = write::gen(namespace, apis, types, opt, header);
David Tolnay7db73692019-10-20 14:51:12 -040052 Ok(out)
53 })() {
David Tolnay7ece56f2020-03-29 21:21:38 -070054 Ok(out) => out.content(),
David Tolnay7db73692019-10-20 14:51:12 -040055 Err(err) => format_err(path, &source, err),
56 }
57}
58
59fn find_bridge_mod(syntax: File) -> Result<Input> {
60 for item in syntax.items {
61 if let Item::Mod(item) = item {
62 for attr in &item.attrs {
63 let path = &attr.path;
64 if quote!(#path).to_string() == "cxx :: bridge" {
65 let module = match item.content {
66 Some(module) => module.1,
67 None => {
68 return Err(Error::Syn(syn::Error::new_spanned(
69 item,
70 Error::OutOfLineMod,
71 )));
72 }
73 };
David Tolnayb6cf3142020-04-19 20:56:09 -070074 let namespace = parse_args(attr)?;
David Tolnay754e21c2020-03-29 20:58:46 -070075 return Ok(Input { namespace, module });
David Tolnay7db73692019-10-20 14:51:12 -040076 }
77 }
78 }
79 }
80 Err(Error::NoBridgeMod)
81}
82
David Tolnayb6cf3142020-04-19 20:56:09 -070083fn parse_args(attr: &Attribute) -> syn::Result<Namespace> {
David Tolnay7db73692019-10-20 14:51:12 -040084 if attr.tokens.is_empty() {
David Tolnayb6cf3142020-04-19 20:56:09 -070085 Ok(Namespace::none())
86 } else {
87 attr.parse_args()
David Tolnay7db73692019-10-20 14:51:12 -040088 }
David Tolnay7db73692019-10-20 14:51:12 -040089}