Skip to content

Commit 5a21b43

Browse files
committed
Run cargo fmt
1 parent 1bcac9b commit 5a21b43

16 files changed

+334
-238
lines changed

src/codegen/mod.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ mod helpers;
22
mod struct_layout;
33

44
use self::helpers::{BlobTyBuilder, attributes};
5+
use self::struct_layout::{StructLayoutTracker, bytes_from_bits_pow2};
56
use self::struct_layout::{align_to, bytes_from_bits};
6-
use self::struct_layout::{bytes_from_bits_pow2, StructLayoutTracker};
77
use aster;
88

99
use ir::annotations::FieldAccessorKind;
@@ -601,8 +601,8 @@ impl CodeGenerator for Type {
601601
ast::TyKind::Path(None, ref p) => {
602602
if used_template_params.is_none() &&
603603
inner_item.expect_type()
604-
.canonical_type(ctx)
605-
.is_enum() &&
604+
.canonical_type(ctx)
605+
.is_enum() &&
606606
p.segments.iter().all(|p| p.parameters.is_none()) {
607607
Some(p.clone())
608608
} else {
@@ -625,7 +625,8 @@ impl CodeGenerator for Type {
625625
let mut generics = typedef.type_(rust_name).generics();
626626
if let Some(ref params) = used_template_params {
627627
for template_param in params {
628-
if let Some(id) = template_param.as_named(ctx, &()) {
628+
if let Some(id) =
629+
template_param.as_named(ctx, &()) {
629630
let template_param = ctx.resolve_type(id);
630631
if template_param.is_invalid_named_type() {
631632
warn!("Item contained invalid template \
@@ -634,7 +635,8 @@ impl CodeGenerator for Type {
634635
return;
635636
}
636637
generics =
637-
generics.ty_param_id(template_param.name().unwrap());
638+
generics.ty_param_id(template_param.name()
639+
.unwrap());
638640
}
639641
}
640642
}
@@ -766,7 +768,7 @@ impl<'a> Bitfield<'a> {
766768
let field_align = field_ty_layout.align;
767769

768770
if field_size_in_bits != 0 &&
769-
(width == 0 || width as usize > unfilled_bits_in_last_unit) {
771+
(width == 0 || width as usize > unfilled_bits_in_last_unit) {
770772
field_size_in_bits = align_to(field_size_in_bits, field_align);
771773
// Push the new field.
772774
let ty =
@@ -896,8 +898,7 @@ impl CodeGenerator for CompInfo {
896898

897899
// generate tuple struct if struct or union is a forward declaration,
898900
// skip for now if template parameters are needed.
899-
if self.is_forward_declaration() &&
900-
used_template_params.is_none() {
901+
if self.is_forward_declaration() && used_template_params.is_none() {
901902
let struct_name = item.canonical_name(ctx);
902903
let struct_name = ctx.rust_ident_raw(&struct_name);
903904
let tuple_struct = quote_item!(ctx.ext_cx(),
@@ -1108,7 +1109,8 @@ impl CodeGenerator for CompInfo {
11081109
} else {
11091110
quote_ty!(ctx.ext_cx(), __BindgenUnionField<$ty>)
11101111
}
1111-
} else if let Some(item) = field_ty.is_incomplete_array(ctx) {
1112+
} else if let Some(item) =
1113+
field_ty.is_incomplete_array(ctx) {
11121114
result.saw_incomplete_array();
11131115

11141116
let inner = item.to_rust_ty(ctx);
@@ -1271,7 +1273,9 @@ impl CodeGenerator for CompInfo {
12711273
}
12721274
} else if !is_union && !self.is_unsized(ctx) {
12731275
if let Some(padding_field) =
1274-
layout.and_then(|layout| struct_layout.pad_struct(&canonical_name, layout)) {
1276+
layout.and_then(|layout| {
1277+
struct_layout.pad_struct(&canonical_name, layout)
1278+
}) {
12751279
fields.push(padding_field);
12761280
}
12771281

@@ -2183,14 +2187,16 @@ impl ToRustTy for Type {
21832187
}
21842188
}
21852189

2186-
let decl_params = if let Some(params) = decl.self_template_params(ctx) {
2190+
let decl_params = if let Some(params) =
2191+
decl.self_template_params(ctx) {
21872192
params
21882193
} else {
21892194
// This can happen if we generated an opaque type for a
21902195
// partial template specialization, in which case we just
21912196
// use the opaque type's layout. If we don't have a layout,
21922197
// we cross our fingers and hope for the best :-/
2193-
debug_assert!(ctx.resolve_type_through_type_refs(decl).is_opaque());
2198+
debug_assert!(ctx.resolve_type_through_type_refs(decl)
2199+
.is_opaque());
21942200
let layout = self.layout(ctx).unwrap_or(Layout::zero());
21952201
ty = BlobTyBuilder::new(layout).build().unwrap();
21962202

@@ -2215,7 +2221,7 @@ impl ToRustTy for Type {
22152221
.map(|(arg, _)| arg.to_rust_ty(ctx))
22162222
.collect::<Vec<_>>();
22172223

2218-
path.segments.last_mut().unwrap().parameters = if
2224+
path.segments.last_mut().unwrap().parameters = if
22192225
template_args.is_empty() {
22202226
None
22212227
} else {
@@ -2250,9 +2256,7 @@ impl ToRustTy for Type {
22502256
inner) {
22512257
ty
22522258
} else {
2253-
utils::build_templated_path(item,
2254-
ctx,
2255-
template_params)
2259+
utils::build_templated_path(item, ctx, template_params)
22562260
}
22572261
}
22582262
TypeKind::Comp(ref info) => {
@@ -2270,10 +2274,13 @@ impl ToRustTy for Type {
22702274
};
22712275
}
22722276

2273-
utils::build_templated_path(item, ctx, template_params.unwrap_or(vec![]))
2277+
utils::build_templated_path(item,
2278+
ctx,
2279+
template_params.unwrap_or(vec![]))
22742280
}
22752281
TypeKind::Opaque => {
2276-
BlobTyBuilder::new(self.layout(ctx).unwrap_or(Layout::zero())).build()
2282+
BlobTyBuilder::new(self.layout(ctx).unwrap_or(Layout::zero()))
2283+
.build()
22772284
}
22782285
TypeKind::BlockPointer => {
22792286
let void = raw_type(ctx, "c_void");

src/codegen/struct_layout.rs

+37-24
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,20 @@ impl<'a, 'ctx> StructLayoutTracker<'a, 'ctx> {
167167
None => return None,
168168
};
169169

170-
if let TypeKind::Array(inner, len) = *field_ty.canonical_type(self.ctx).kind() {
170+
if let TypeKind::Array(inner, len) =
171+
*field_ty.canonical_type(self.ctx).kind() {
171172
// FIXME(emilio): As an _ultra_ hack, we correct the layout returned
172173
// by arrays of structs that have a bigger alignment than what we
173174
// can support.
174175
//
175176
// This means that the structs in the array are super-unsafe to
176177
// access, since they won't be properly aligned, but *shrug*.
177-
if let Some(layout) = self.ctx.resolve_type(inner).layout(self.ctx) {
178+
if let Some(layout) = self.ctx
179+
.resolve_type(inner)
180+
.layout(self.ctx) {
178181
if layout.align > mem::size_of::<*mut ()>() {
179-
field_layout.size =
180-
align_to(layout.size, layout.align) * len;
182+
field_layout.size = align_to(layout.size, layout.align) *
183+
len;
181184
field_layout.align = mem::size_of::<*mut ()>();
182185
}
183186
}
@@ -197,7 +200,8 @@ impl<'a, 'ctx> StructLayoutTracker<'a, 'ctx> {
197200
};
198201

199202
// Otherwise the padding is useless.
200-
let need_padding = padding_bytes >= field_layout.align || field_layout.align > mem::size_of::<*mut ()>();
203+
let need_padding = padding_bytes >= field_layout.align ||
204+
field_layout.align > mem::size_of::<*mut ()>();
201205

202206
self.latest_offset += padding_bytes;
203207

@@ -206,22 +210,25 @@ impl<'a, 'ctx> StructLayoutTracker<'a, 'ctx> {
206210
self.latest_offset);
207211

208212
debug!("align field {} to {}/{} with {} padding bytes {:?}",
209-
field_name,
210-
self.latest_offset,
211-
field_offset.unwrap_or(0) / 8,
212-
padding_bytes,
213-
field_layout);
213+
field_name,
214+
self.latest_offset,
215+
field_offset.unwrap_or(0) / 8,
216+
padding_bytes,
217+
field_layout);
214218

215219
if need_padding && padding_bytes != 0 {
216-
Some(Layout::new(padding_bytes, cmp::min(field_layout.align, mem::size_of::<*mut ()>())))
220+
Some(Layout::new(padding_bytes,
221+
cmp::min(field_layout.align,
222+
mem::size_of::<*mut ()>())))
217223
} else {
218224
None
219225
}
220226
};
221227

222228
self.latest_offset += field_layout.size;
223229
self.latest_field_layout = Some(field_layout);
224-
self.max_field_align = cmp::max(self.max_field_align, field_layout.align);
230+
self.max_field_align = cmp::max(self.max_field_align,
231+
field_layout.align);
225232
self.last_field_was_bitfield = false;
226233

227234
debug!("Offset: {}: {} -> {}",
@@ -232,11 +239,15 @@ impl<'a, 'ctx> StructLayoutTracker<'a, 'ctx> {
232239
padding_layout.map(|layout| self.padding_field(layout))
233240
}
234241

235-
pub fn pad_struct(&mut self, name: &str, layout: Layout) -> Option<ast::StructField> {
242+
pub fn pad_struct(&mut self,
243+
name: &str,
244+
layout: Layout)
245+
-> Option<ast::StructField> {
236246
if layout.size < self.latest_offset {
237247
error!("Calculated wrong layout for {}, too more {} bytes",
238-
name, self.latest_offset - layout.size);
239-
return None
248+
name,
249+
self.latest_offset - layout.size);
250+
return None;
240251
}
241252

242253
let padding_bytes = layout.size - self.latest_offset;
@@ -248,14 +259,14 @@ impl<'a, 'ctx> StructLayoutTracker<'a, 'ctx> {
248259
// regardless, because bitfields don't respect alignment as strictly as
249260
// other fields.
250261
if padding_bytes > 0 &&
251-
(padding_bytes >= layout.align ||
252-
(self.last_field_was_bitfield &&
253-
padding_bytes >= self.latest_field_layout.unwrap().align) ||
254-
layout.align > mem::size_of::<*mut ()>()) {
262+
(padding_bytes >= layout.align ||
263+
(self.last_field_was_bitfield &&
264+
padding_bytes >= self.latest_field_layout.unwrap().align) ||
265+
layout.align > mem::size_of::<*mut ()>()) {
255266
let layout = if self.comp.packed() {
256267
Layout::new(padding_bytes, 1)
257268
} else if self.last_field_was_bitfield ||
258-
layout.align > mem::size_of::<*mut ()>() {
269+
layout.align > mem::size_of::<*mut ()>() {
259270
// We've already given up on alignment here.
260271
Layout::for_size(padding_bytes)
261272
} else {
@@ -316,12 +327,14 @@ impl<'a, 'ctx> StructLayoutTracker<'a, 'ctx> {
316327

317328
// If it was, we may or may not need to align, depending on what the
318329
// current field alignment and the bitfield size and alignment are.
319-
debug!("align_to_bitfield? {}: {:?} {:?}", self.last_field_was_bitfield,
320-
layout, new_field_layout);
330+
debug!("align_to_bitfield? {}: {:?} {:?}",
331+
self.last_field_was_bitfield,
332+
layout,
333+
new_field_layout);
321334

322335
if self.last_field_was_bitfield &&
323-
new_field_layout.align <= layout.size % layout.align &&
324-
new_field_layout.size <= layout.size % layout.align {
336+
new_field_layout.align <= layout.size % layout.align &&
337+
new_field_layout.size <= layout.size % layout.align {
325338
// The new field will be coalesced into some of the remaining bits.
326339
//
327340
// FIXME(emilio): I think this may not catch everything?

src/ir/comp.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl CompInfo {
386386
use std::cmp;
387387
// We can't do better than clang here, sorry.
388388
if self.kind == CompKind::Struct {
389-
return None
389+
return None;
390390
}
391391

392392
let mut max_size = 0;
@@ -619,10 +619,8 @@ impl CompInfo {
619619
BaseKind::Normal
620620
};
621621

622-
let type_id = Item::from_ty_or_ref(cur.cur_type(),
623-
cur,
624-
None,
625-
ctx);
622+
let type_id =
623+
Item::from_ty_or_ref(cur.cur_type(), cur, None, ctx);
626624
ci.base_members.push(Base {
627625
ty: type_id,
628626
kind: kind,
@@ -800,7 +798,9 @@ impl CompInfo {
800798
}
801799

802800
impl TemplateDeclaration for CompInfo {
803-
fn self_template_params(&self, _ctx: &BindgenContext) -> Option<Vec<ItemId>> {
801+
fn self_template_params(&self,
802+
_ctx: &BindgenContext)
803+
-> Option<Vec<ItemId>> {
804804
if self.template_params.is_empty() {
805805
None
806806
} else {

src/ir/context.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::int::IntKind;
55
use super::item::{Item, ItemCanonicalPath, ItemSet};
66
use super::item_kind::ItemKind;
77
use super::module::{Module, ModuleKind};
8-
use super::named::{analyze, UsedTemplateParameters};
8+
use super::named::{UsedTemplateParameters, analyze};
99
use super::template::TemplateInstantiation;
1010
use super::traversal::{self, Edge, ItemTraversal};
1111
use super::ty::{FloatKind, TemplateDeclaration, Type, TypeKind};
@@ -250,8 +250,7 @@ impl<'ctx> BindgenContext<'ctx> {
250250
item,
251251
declaration,
252252
location);
253-
debug_assert!(declaration.is_some() ||
254-
!item.kind().is_type() ||
253+
debug_assert!(declaration.is_some() || !item.kind().is_type() ||
255254
item.kind().expect_type().is_builtin_or_named() ||
256255
item.kind().expect_type().is_opaque(),
257256
"Adding a type without declaration?");
@@ -323,7 +322,8 @@ impl<'ctx> BindgenContext<'ctx> {
323322

324323
assert!(item.expect_type().is_named(),
325324
"Should directly be a named type, not a resolved reference or anything");
326-
assert_eq!(definition.kind(), clang_sys::CXCursor_TemplateTypeParameter);
325+
assert_eq!(definition.kind(),
326+
clang_sys::CXCursor_TemplateTypeParameter);
327327

328328
let id = item.id();
329329
let old_item = self.items.insert(id, item);
@@ -338,7 +338,8 @@ impl<'ctx> BindgenContext<'ctx> {
338338
/// Get the named type defined at the given cursor location, if we've
339339
/// already added one.
340340
pub fn get_named_type(&self, definition: &clang::Cursor) -> Option<ItemId> {
341-
assert_eq!(definition.kind(), clang_sys::CXCursor_TemplateTypeParameter);
341+
assert_eq!(definition.kind(),
342+
clang_sys::CXCursor_TemplateTypeParameter);
342343
self.named_types.get(definition).cloned()
343344
}
344345

@@ -611,8 +612,8 @@ impl<'ctx> BindgenContext<'ctx> {
611612
for id in self.whitelisted_items() {
612613
used_params.entry(id)
613614
.or_insert(id.self_template_params(self)
614-
.map_or(Default::default(),
615-
|params| params.into_iter().collect()));
615+
.map_or(Default::default(),
616+
|params| params.into_iter().collect()));
616617
}
617618
self.used_template_parameters = Some(used_params);
618619
}
@@ -624,7 +625,10 @@ impl<'ctx> BindgenContext<'ctx> {
624625
/// This method may only be called during the codegen phase, because the
625626
/// template usage information is only computed as we enter the codegen
626627
/// phase.
627-
pub fn uses_template_parameter(&self, item: ItemId, template_param: ItemId) -> bool {
628+
pub fn uses_template_parameter(&self,
629+
item: ItemId,
630+
template_param: ItemId)
631+
-> bool {
628632
assert!(self.in_codegen_phase(),
629633
"We only compute template parameter usage as we enter codegen");
630634

@@ -1430,7 +1434,9 @@ impl PartialType {
14301434
}
14311435

14321436
impl TemplateDeclaration for PartialType {
1433-
fn self_template_params(&self, _ctx: &BindgenContext) -> Option<Vec<ItemId>> {
1437+
fn self_template_params(&self,
1438+
_ctx: &BindgenContext)
1439+
-> Option<Vec<ItemId>> {
14341440
// Maybe at some point we will eagerly parse named types, but for now we
14351441
// don't and this information is unavailable.
14361442
None

0 commit comments

Comments
 (0)