From 61743aa190772a1c75acb75d274215274d0e6873 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Tue, 22 Aug 2017 18:53:55 -0700 Subject: 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 Finished dev [unoptimized + debuginfo] target(s) in 98.75 secs ``` After this commit: ``` $ cargo clean; cargo build 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 --- src/codegen/struct_layout.rs | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) (limited to 'src/codegen/struct_layout.rs') 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, - ) -> Option { + ) -> Option { 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 { + pub fn pad_struct(&mut self, layout: Layout) -> Option { 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 { + pub fn align_struct(&self, layout: Layout) -> Option { 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. -- cgit v1.2.3