diff options
author | Nick Fitzgerald <fitzgen@gmail.com> | 2017-08-22 18:53:55 -0700 |
---|---|---|
committer | Nick Fitzgerald <fitzgen@gmail.com> | 2017-09-07 10:52:31 -0700 |
commit | 61743aa190772a1c75acb75d274215274d0e6873 (patch) | |
tree | ca06c845c4ce20dd7ad1022c5bd28da8ff888cc0 /src/codegen/struct_layout.rs | |
parent | 7346811ab5238feab406d6aee858197fa5254fcb (diff) |
Use `quote` instead of `syntex` for Rust code generation
The `syntex` crate is unmaintained. It is slow to build, and additionally it
requires that we pre-process `src/codegen/mod.rs` before we build the `bindgen`
crate.
The `quote` crate provides similar quasi-quoting functionality, is maintained,
and builds faster. It doesn't have a typed API or builders, however; it only
deals with tokens.
Before this commit:
```
$ cargo clean; cargo build
<snip>
Finished dev [unoptimized + debuginfo] target(s) in 98.75 secs
```
After this commit:
```
$ cargo clean; cargo build
<snip>
Finished dev [unoptimized + debuginfo] target(s) in 46.26 secs
```
Build time is cut in half! But what about run time?
Before this commit:
```
Generated Stylo bindings in: Duration { secs: 3, nanos: 521105668 }
```
After this commit:
```
Generated Stylo bindings in: Duration { secs: 3, nanos: 548797242 }
```
So it appears to be about 20ms slower at generating Stylo bindings, but I
suspect this is well within the noise.
Finally, this also lets us remove that nasty `mem::transmute` inside
`bindgen::ir::BindgenContext::gen` that was used for the old `syntex`
context. Now `BindgenContext` doesn't have a lifetime parameter either. This
should make it easier to revisit doing our analyses in parallel with `rayon`,
since that context was one of the things that made it hard for `BindgenContext`
to implement `Sync`.
Fixes #925
Diffstat (limited to 'src/codegen/struct_layout.rs')
-rw-r--r-- | src/codegen/struct_layout.rs | 37 |
1 files changed, 16 insertions, 21 deletions
diff --git a/src/codegen/struct_layout.rs b/src/codegen/struct_layout.rs index 956a1f44..06059853 100644 --- a/src/codegen/struct_layout.rs +++ b/src/codegen/struct_layout.rs @@ -2,22 +2,19 @@ use super::helpers; -use aster::struct_field::StructFieldBuilder; - use ir::comp::CompInfo; use ir::context::BindgenContext; use ir::layout::Layout; use ir::ty::{Type, TypeKind}; +use quote; use std::cmp; use std::mem; -use syntax::ast; - /// Trace the layout of struct. #[derive(Debug)] -pub struct StructLayoutTracker<'a, 'ctx: 'a> { +pub struct StructLayoutTracker<'a> { name: &'a str, - ctx: &'a BindgenContext<'ctx>, + ctx: &'a BindgenContext, comp: &'a CompInfo, latest_offset: usize, padding_count: usize, @@ -80,9 +77,9 @@ fn test_bytes_from_bits_pow2() { } } -impl<'a, 'ctx> StructLayoutTracker<'a, 'ctx> { +impl<'a> StructLayoutTracker<'a> { pub fn new( - ctx: &'a BindgenContext<'ctx>, + ctx: &'a BindgenContext, comp: &'a CompInfo, name: &'a str, ) -> Self { @@ -154,7 +151,7 @@ impl<'a, 'ctx> StructLayoutTracker<'a, 'ctx> { field_name: &str, field_ty: &Type, field_offset: Option<usize>, - ) -> Option<ast::StructField> { + ) -> Option<quote::Tokens> { let mut field_layout = match field_ty.layout(self.ctx) { Some(l) => l, None => return None, @@ -241,7 +238,7 @@ impl<'a, 'ctx> StructLayoutTracker<'a, 'ctx> { padding_layout.map(|layout| self.padding_field(layout)) } - pub fn pad_struct(&mut self, layout: Layout) -> Option<ast::StructField> { + pub fn pad_struct(&mut self, layout: Layout) -> Option<quote::Tokens> { debug!( "pad_struct:\n\tself = {:#?}\n\tlayout = {:#?}", self, @@ -291,17 +288,15 @@ impl<'a, 'ctx> StructLayoutTracker<'a, 'ctx> { } } - pub fn align_struct(&self, layout: Layout) -> Option<ast::StructField> { + pub fn align_struct(&self, layout: Layout) -> Option<quote::Tokens> { if self.max_field_align < layout.align && layout.align <= mem::size_of::<*mut ()>() { let ty = helpers::blob(Layout::new(0, layout.align)); - Some( - StructFieldBuilder::named("__bindgen_align") - .pub_() - .build_ty(ty), - ) + Some(quote! { + pub __bindgen_align: #ty , + }) } else { None } @@ -311,19 +306,19 @@ impl<'a, 'ctx> StructLayoutTracker<'a, 'ctx> { align_to(self.latest_offset, layout.align) - self.latest_offset } - fn padding_field(&mut self, layout: Layout) -> ast::StructField { + fn padding_field(&mut self, layout: Layout) -> quote::Tokens { let ty = helpers::blob(layout); let padding_count = self.padding_count; self.padding_count += 1; - let padding_field_name = format!("__bindgen_padding_{}", padding_count); + let padding_field_name = quote::Ident::new(format!("__bindgen_padding_{}", padding_count)); self.max_field_align = cmp::max(self.max_field_align, layout.align); - StructFieldBuilder::named(padding_field_name) - .pub_() - .build_ty(ty) + quote! { + pub #padding_field_name : #ty , + } } /// Returns whether the new field is known to merge with a bitfield. |