summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNick Fitzgerald <fitzgen@gmail.com>2017-10-12 12:02:47 -0700
committerNick Fitzgerald <fitzgen@gmail.com>2017-10-12 13:32:09 -0700
commit6f401c5ef4f082ed73c28cc53a57912e9352b349 (patch)
tree3d236acd59fda63651d04259483251572150e145 /src
parent624aef86cad30f7dc108e8d0013bc7cb562e3e8a (diff)
Make `ir::BindgenContext::gen` take ownership of `self`
`bindgen` follows a pipeline architecture, and we only ever generate bindings once. By taking ownership of `self`, we can enforce this. We can also remove checks inside `gen` for whether we have resolved type refs or not, since we now know that we haven't because it is guaranteed to only be called the one time.
Diffstat (limited to 'src')
-rw-r--r--src/codegen/mod.rs4
-rw-r--r--src/ir/context.rs19
-rw-r--r--src/lib.rs20
3 files changed, 21 insertions, 22 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index 042373f3..317604f0 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -7,6 +7,8 @@ pub mod struct_layout;
use self::helpers::attributes;
use self::struct_layout::StructLayoutTracker;
+use super::BindgenOptions;
+
use ir::analysis::HasVtable;
use ir::annotations::FieldAccessorKind;
use ir::comment;
@@ -3327,7 +3329,7 @@ impl CodeGenerator for ObjCInterface {
}
}
-pub fn codegen(context: &mut BindgenContext) -> Vec<quote::Tokens> {
+pub(crate) fn codegen(context: BindgenContext) -> (Vec<quote::Tokens>, BindgenOptions) {
context.gen(|context| {
let _t = context.timer("codegen");
let counter = Cell::new(0);
diff --git a/src/ir/context.rs b/src/ir/context.rs
index c84e66ca..560f6ec2 100644
--- a/src/ir/context.rs
+++ b/src/ir/context.rs
@@ -1152,17 +1152,15 @@ impl BindgenContext {
/// Enter the code generation phase, invoke the given callback `cb`, and
/// leave the code generation phase.
- pub fn gen<F, Out>(&mut self, cb: F) -> Out
+ pub(crate) fn gen<F, Out>(mut self, cb: F) -> (Out, BindgenOptions)
where
F: FnOnce(&Self) -> Out,
{
self.in_codegen = true;
- if !self.collected_typerefs() {
- self.resolve_typerefs();
- self.compute_bitfield_units();
- self.process_replacements();
- }
+ self.resolve_typerefs();
+ self.compute_bitfield_units();
+ self.process_replacements();
self.deanonymize_fields();
@@ -1189,9 +1187,8 @@ impl BindgenContext {
self.compute_cannot_derive_hash();
self.compute_cannot_derive_partialord_partialeq_or_eq();
- let ret = cb(self);
- self.in_codegen = false;
- ret
+ let ret = cb(&self);
+ (ret, self.options)
}
/// When the `testing_only_extra_assertions` feature is enabled, this
@@ -1416,9 +1413,9 @@ impl BindgenContext {
}
/// Resolve a function with the given id.
- ///
+ ///
/// Panics if there is no item for the given `FunctionId` or if the resolved
- /// item is not a `Function`.
+ /// item is not a `Function`.
pub fn resolve_func(&self, func_id: FunctionId) -> &Function {
self.resolve_item(func_id).kind().expect_function()
}
diff --git a/src/lib.rs b/src/lib.rs
index a945b3e1..6e4ba7aa 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -858,7 +858,7 @@ impl Builder {
}
/// Set whether `Ord` should be derived by default.
- /// We can't compute `Ord` without computing `PartialOrd`,
+ /// We can't compute `Ord` without computing `PartialOrd`,
/// so we set the same option to derive_partialord.
pub fn derive_ord(mut self, doit: bool) -> Self {
self.options.derive_ord = doit;
@@ -1475,7 +1475,7 @@ fn ensure_libclang_is_loaded() {
/// Generated Rust bindings.
#[derive(Debug)]
pub struct Bindings {
- context: BindgenContext,
+ options: BindgenOptions,
module: quote::Tokens,
}
@@ -1559,10 +1559,10 @@ impl Bindings {
try!(parse(&mut context));
}
- let items = codegen::codegen(&mut context);
+ let (items, options) = codegen::codegen(context);
Ok(Bindings {
- context: context,
+ options: options,
module: quote! {
#( #items )*
}
@@ -1600,11 +1600,11 @@ impl Bindings {
"/* automatically generated by rust-bindgen */\n\n".as_bytes(),
)?;
- for line in self.context.options().raw_lines.iter() {
+ for line in self.options.raw_lines.iter() {
writer.write(line.as_bytes())?;
writer.write("\n".as_bytes())?;
}
- if !self.context.options().raw_lines.is_empty() {
+ if !self.options.raw_lines.is_empty() {
writer.write("\n".as_bytes())?;
}
@@ -1614,9 +1614,10 @@ impl Bindings {
/// Checks if rustfmt_bindings is set and runs rustfmt on the file
fn rustfmt_generated_file(&self, file: &Path) -> io::Result<()> {
- let _t = self.context.timer("rustfmt_generated_file");
+ let _t = time::Timer::new("rustfmt_generated_file")
+ .with_output(self.options.time_phases);
- if !self.context.options().rustfmt_bindings {
+ if !self.options.rustfmt_bindings {
return Ok(());
}
@@ -1629,8 +1630,7 @@ impl Bindings {
let mut cmd = Command::new(rustfmt);
- if let Some(path) = self.context
- .options()
+ if let Some(path) = self.options
.rustfmt_configuration_file
.as_ref()
.and_then(|f| f.to_str())