-
Notifications
You must be signed in to change notification settings - Fork 729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using syn
for code generation instead of quote
#2251
Comments
@emilio surely knows a lot more about the tradeoffs than I do. Personally, I have only ever written ordinary macros, and no procedural ones, so my opinion should not carry much weight, but to the level that I understand this issue's proposal, it sounds worthwhile to me.
An incremental approach, using relatively small, reversible changes, is a strongly favorable thing for me. |
Hi @kulp thanks for you answer :)
Yeah I think this is definitely possible, We could take functions and self-contained sections of code like this: fn top_level_path(
ctx: &BindgenContext,
item: &Item,
) -> Vec<proc_macro2::TokenStream> {
let mut path = vec![quote! { self }];
if ctx.options().enable_cxx_namespaces {
for _ in 0..item.codegen_depth(ctx) {
path.push(quote! { super });
}
}
path
} and replace them by fn top_level_path(
ctx: &BindgenContext,
item: &Item,
) -> Path {
let mut segments = Punctuated::<PathSegment, Colon2>::new();
segments.push(parse_quote!(self));
if ctx.options().enable_cxx_namespaces {
for _ in 0..item.codegen_depth(ctx) {
path.push(parse_quote!(super));
}
}
Path {
leading_colon: None,
segments,
}
} Then we can replace each use of One aspect I'm not sure about is performance issues so we would also have to consider this. |
Can you elaborate what use cases do you envision for this? And why are they not addressable for example if you use |
it would be definitely possible to use the output of |
That being said, moving to |
Given that #2254 is close to being merged I'm marking this as solved |
This is a rather loose proposal but I'd like to start this discussion as it might provide a more straightforward way to handle issues like #564 and #1743. I've seen that most of the code generation is done relying on
quote!
which is a very straightforward and simple way to emit rust code.However, tacking any issue that requires reordering or modifying the generated code would require several workarounds due to the unstructured nature of
proc_macro2::TokenStream
.I was wondering if it would be feasible and useful use
syn::parse_quote!
and other tools provided by thesyn
crate for code generation in order to keep all the generated code in a more structured way before emitting it sobindgen
can manipulate it in later stages in an easier way.cc @amanjeev
Edit: I forgot to mention that one advantage of being able to handle all the items generated by
bindgen
assyn
types is that we could implement "passes" in a modular way so we can easily enable/disable them. So for example, we could deduplicateextern "C" { ... }
blocks in one pass and independently write another pass to reorganize items. In general, it would help a lot when having to handle generated code in my opinion.Another important point is that we should be able to do this transition incrementally as we can always produce
TokenStream
s from thesyn
data structures by using theToToken
trait.However, we think it's pretty important to know if this change would be worth it from the maintainers point of view or if a less intrusive way to solve the issues mentioned above would be better.
The text was updated successfully, but these errors were encountered: