Skip to content

Move self into ir::BindgenContext::gen #1079

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

Merged
merged 2 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 8 additions & 11 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
22 changes: 11 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1475,7 +1475,7 @@ fn ensure_libclang_is_loaded() {
/// Generated Rust bindings.
#[derive(Debug)]
pub struct Bindings {
context: BindgenContext,
options: BindgenOptions,
module: quote::Tokens,
}

Expand Down Expand Up @@ -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 )*
}
Expand Down Expand Up @@ -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())?;
}

Expand All @@ -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(());
}

Expand All @@ -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())
Expand Down Expand Up @@ -1677,7 +1677,7 @@ fn filter_builtins(ctx: &BindgenContext, cursor: &clang::Cursor) -> bool {
}

/// Parse one `Item` from the Clang cursor.
pub fn parse_one(
fn parse_one(
ctx: &mut BindgenContext,
cursor: clang::Cursor,
parent: Option<ItemId>,
Expand Down