Skip to content

Commit ddb319c

Browse files
committed
split processing module
1 parent 06a6479 commit ddb319c

File tree

4 files changed

+136
-128
lines changed

4 files changed

+136
-128
lines changed

src/codegen/postprocessing.rs

Lines changed: 0 additions & 128 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use syn::{Item, ItemForeignMod};
2+
3+
pub(super) fn merge_extern_blocks(items: &mut Vec<Item>) {
4+
// Keep all the extern blocks in a different `Vec` for faster search.
5+
let mut foreign_mods = Vec::<ItemForeignMod>::new();
6+
7+
for item in std::mem::take(items) {
8+
match item {
9+
Item::ForeignMod(ItemForeignMod {
10+
attrs,
11+
abi,
12+
brace_token,
13+
items: foreign_items,
14+
}) => {
15+
let mut exists = false;
16+
for foreign_mod in &mut foreign_mods {
17+
// Check if there is a extern block with the same ABI and
18+
// attributes.
19+
if foreign_mod.attrs == attrs && foreign_mod.abi == abi {
20+
// Merge the items of the two blocks.
21+
foreign_mod.items.extend_from_slice(&foreign_items);
22+
exists = true;
23+
break;
24+
}
25+
}
26+
// If no existing extern block had the same ABI and attributes, store
27+
// it.
28+
if !exists {
29+
foreign_mods.push(ItemForeignMod {
30+
attrs,
31+
abi,
32+
brace_token,
33+
items: foreign_items,
34+
});
35+
}
36+
}
37+
// If the item is not an extern block, we don't have to do anything.
38+
_ => items.push(item),
39+
}
40+
}
41+
42+
// Move all the extern blocks alongside the rest of the items.
43+
for foreign_mod in foreign_mods {
44+
items.push(Item::ForeignMod(foreign_mod));
45+
}
46+
}

src/codegen/postprocessing/mod.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use proc_macro2::TokenStream;
2+
use quote::ToTokens;
3+
use syn::Item;
4+
5+
use crate::BindgenOptions;
6+
7+
mod merge_extern_blocks;
8+
mod sort_semantically;
9+
10+
use merge_extern_blocks::merge_extern_blocks;
11+
use sort_semantically::sort_semantically;
12+
13+
struct PostProcessingPass {
14+
should_run: fn(&BindgenOptions) -> bool,
15+
run: fn(&mut Vec<Item>),
16+
}
17+
18+
// TODO: This can be a const fn when mutable references are allowed in const
19+
// context.
20+
macro_rules! pass {
21+
($pass:ident) => {
22+
PostProcessingPass {
23+
should_run: |options| options.$pass,
24+
run: |items| $pass(items),
25+
}
26+
};
27+
}
28+
29+
const PASSES: &[PostProcessingPass] =
30+
&[pass!(merge_extern_blocks), pass!(sort_semantically)];
31+
32+
pub(crate) fn postprocessing(
33+
items: Vec<TokenStream>,
34+
options: &BindgenOptions,
35+
) -> TokenStream {
36+
let require_syn = PASSES.iter().any(|pass| (pass.should_run)(options));
37+
if !require_syn {
38+
return items.into_iter().collect();
39+
}
40+
let module_wrapped_tokens =
41+
quote!(mod wrapper_for_sorting_hack { #( #items )* });
42+
43+
// This syn business is a hack, for now. This means that we are re-parsing already
44+
// generated code using `syn` (as opposed to `quote`) because `syn` provides us more
45+
// control over the elements.
46+
// One caveat is that some of the items coming from `quote`d output might have
47+
// multiple items within them. Hence, we have to wrap the incoming in a `mod`.
48+
// The two `unwrap`s here are deliberate because
49+
// The first one won't panic because we build the `mod` and know it is there
50+
// The second one won't panic because we know original output has something in
51+
// it already.
52+
let (_, mut items) = syn::parse2::<syn::ItemMod>(module_wrapped_tokens)
53+
.unwrap()
54+
.content
55+
.unwrap();
56+
57+
for pass in PASSES {
58+
if (pass.should_run)(options) {
59+
(pass.run)(&mut items);
60+
}
61+
}
62+
63+
let synful_items = items.into_iter().map(|item| item.into_token_stream());
64+
65+
quote! { #( #synful_items )* }
66+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use syn::Item;
2+
3+
pub(super) fn sort_semantically(items: &mut [Item]) {
4+
items.sort_by_key(|item| match item {
5+
Item::Type(_) => 0,
6+
Item::Struct(_) => 1,
7+
Item::Const(_) => 2,
8+
Item::Fn(_) => 3,
9+
Item::Enum(_) => 4,
10+
Item::Union(_) => 5,
11+
Item::Static(_) => 6,
12+
Item::Trait(_) => 7,
13+
Item::TraitAlias(_) => 8,
14+
Item::Impl(_) => 9,
15+
Item::Mod(_) => 10,
16+
Item::Use(_) => 11,
17+
Item::Verbatim(_) => 12,
18+
Item::ExternCrate(_) => 13,
19+
Item::ForeignMod(_) => 14,
20+
Item::Macro(_) => 15,
21+
Item::Macro2(_) => 16,
22+
_ => 18,
23+
});
24+
}

0 commit comments

Comments
 (0)