Skip to content

Commit edc8b7f

Browse files
committed
Apply autocxx-specific changes after merge
1 parent ec43ebf commit edc8b7f

File tree

7 files changed

+110
-81
lines changed

7 files changed

+110
-81
lines changed

src/codegen/helpers.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use crate::ir::comp::SpecialMemberKind;
44
use crate::ir::function::Visibility;
5-
use crate::{ir::context::BindgenContext, BindgenOptions};
65
use crate::ir::layout::Layout;
6+
use crate::{ir::context::BindgenContext, BindgenOptions};
77
use proc_macro2::{Ident, Span, TokenStream};
88
use quote::TokenStreamExt;
99

@@ -72,18 +72,15 @@ pub mod attributes {
7272
}
7373
}
7474

75-
7675
pub trait CppSemanticAttributeCreator {
7776
fn do_add(&mut self, ts: TokenStream);
7877
fn is_enabled(&self) -> bool;
7978

8079
fn add(&mut self, tokens: TokenStream) {
8180
if self.is_enabled() {
82-
self.do_add(
83-
quote! {
84-
#[cpp_semantics(#tokens)]
85-
}
86-
)
81+
self.do_add(quote! {
82+
#[cpp_semantics(#tokens)]
83+
})
8784
}
8885
}
8986

@@ -135,7 +132,7 @@ pub trait CppSemanticAttributeCreator {
135132
self.add_ident("rvalue_reference")
136133
}
137134

138-
fn is_virtual(&mut self, ) {
135+
fn is_virtual(&mut self) {
139136
self.add_ident("bindgen_virtual")
140137
}
141138

@@ -145,7 +142,7 @@ pub trait CppSemanticAttributeCreator {
145142
})
146143
}
147144

148-
fn is_pure_virtual(&mut self, ) {
145+
fn is_pure_virtual(&mut self) {
149146
self.add_ident("pure_virtual")
150147
}
151148

@@ -185,13 +182,17 @@ pub trait CppSemanticAttributeCreator {
185182

186183
pub struct CppSemanticAttributeAdder<'a> {
187184
enabled: bool,
188-
attrs: &'a mut Vec<TokenStream>
185+
attrs: &'a mut Vec<TokenStream>,
189186
}
190187

191188
impl<'a> CppSemanticAttributeAdder<'a> {
192-
pub(crate) fn new(opts: &BindgenOptions, attrs: &'a mut Vec<TokenStream>) -> Self {
189+
pub(crate) fn new(
190+
opts: &BindgenOptions,
191+
attrs: &'a mut Vec<TokenStream>,
192+
) -> Self {
193193
Self {
194-
enabled: opts.cpp_semantic_attributes, attrs
194+
enabled: opts.cpp_semantic_attributes,
195+
attrs,
195196
}
196197
}
197198
}
@@ -206,16 +207,16 @@ impl<'a> CppSemanticAttributeCreator for CppSemanticAttributeAdder<'a> {
206207
}
207208
}
208209

209-
210210
pub struct CppSemanticAttributeSingle {
211211
enabled: bool,
212-
attr: TokenStream
212+
attr: TokenStream,
213213
}
214214

215215
impl CppSemanticAttributeSingle {
216216
pub(crate) fn new(opts: &BindgenOptions) -> Self {
217217
Self {
218-
enabled: opts.cpp_semantic_attributes, attr: quote! {}
218+
enabled: opts.cpp_semantic_attributes,
219+
attr: quote! {},
219220
}
220221
}
221222

@@ -310,8 +311,8 @@ pub mod ast_ty {
310311
}
311312
}
312313
None => {
313-
if ctx.options().use_core
314-
&& ctx.options().rust_features.core_ffi_c_void
314+
if ctx.options().use_core &&
315+
ctx.options().rust_features.core_ffi_c_void
315316
{
316317
quote! { ::core::ffi::c_void }
317318
} else {

src/codegen/mod.rs

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ use self::struct_layout::StructLayoutTracker;
1717

1818
use super::BindgenOptions;
1919

20-
use crate::codegen::helpers::{CppSemanticAttributeAdder, CppSemanticAttributeCreator, CppSemanticAttributeSingle};
20+
use crate::codegen::helpers::{
21+
CppSemanticAttributeAdder, CppSemanticAttributeCreator,
22+
CppSemanticAttributeSingle,
23+
};
2124
use crate::ir::analysis::{HasVtable, Sizedness};
2225
use crate::ir::annotations::FieldAccessorKind;
2326
use crate::ir::comment;
@@ -898,7 +901,8 @@ impl CodeGenerator for Type {
898901
} else {
899902
quote! {}
900903
};
901-
let mut semantic_annotations = CppSemanticAttributeSingle::new(ctx.options());
904+
let mut semantic_annotations =
905+
CppSemanticAttributeSingle::new(ctx.options());
902906
// We are handling (essentially) type X = Y;
903907
// We want to denote only if X has unused template params, e.g.
904908
// type X<A> = Y;
@@ -944,7 +948,11 @@ impl CodeGenerator for Type {
944948
let mut attributes = Vec::new();
945949
if let Some(original_name) = item.original_name(ctx) {
946950
if name != original_name {
947-
let mut semantic_annotations = CppSemanticAttributeAdder::new(ctx.options(), &mut attributes);
951+
let mut semantic_annotations =
952+
CppSemanticAttributeAdder::new(
953+
ctx.options(),
954+
&mut attributes,
955+
);
948956
semantic_annotations.original_name(&original_name);
949957
}
950958
}
@@ -1112,8 +1120,8 @@ impl<'a> CodeGenerator for Vtable<'a> {
11121120

11131121
// FIXME: Need to account for overloading with times_seen (separately from regular function path).
11141122
let function_name = ctx.rust_ident(function_name);
1115-
let mut args = utils::fnsig_arguments(ctx, signature);
1116-
let ret = utils::fnsig_return_ty(ctx, signature);
1123+
let (mut args, _) = utils::fnsig_arguments(ctx, signature);
1124+
let (ret, _) = utils::fnsig_return_ty(ctx, signature);
11171125

11181126
args[0] = if m.is_const() {
11191127
quote! { this: *const #class_ident }
@@ -1338,9 +1346,7 @@ impl<'a> FieldCodegen<'a> for FieldData {
13381346
let field_item =
13391347
self.ty().into_resolver().through_type_refs().resolve(ctx);
13401348
let field_ty = field_item.expect_type();
1341-
let ty = self
1342-
.ty()
1343-
.to_rust_ty_or_opaque(ctx, &());
1349+
let ty = self.ty().to_rust_ty_or_opaque(ctx, &());
13441350
let (mut ty, ty_annotations) = ty.to_outer_type();
13451351
ty.append_implicit_template_params(ctx, field_item);
13461352

@@ -1406,10 +1412,13 @@ impl<'a> FieldCodegen<'a> for FieldData {
14061412
self.annotations().accessor_kind().unwrap_or(accessor_kind);
14071413

14081414
let mut attributes = Vec::new();
1409-
let mut csaa = CppSemanticAttributeAdder::new(ctx.options(), &mut attributes);
1415+
let mut csaa =
1416+
CppSemanticAttributeAdder::new(ctx.options(), &mut attributes);
14101417
match ty_annotations {
14111418
RustTyAnnotation::Reference => csaa.field_type_reference(),
1412-
RustTyAnnotation::RValueReference => csaa.field_type_rvalue_reference(),
1419+
RustTyAnnotation::RValueReference => {
1420+
csaa.field_type_rvalue_reference()
1421+
}
14131422
_ => {}
14141423
};
14151424

@@ -2076,7 +2085,8 @@ impl CodeGenerator for CompInfo {
20762085
} else {
20772086
attributes.push(attributes::repr("C"));
20782087
}
2079-
let mut semantic_annotations = CppSemanticAttributeAdder::new(ctx.options(), &mut attributes);
2088+
let mut semantic_annotations =
2089+
CppSemanticAttributeAdder::new(ctx.options(), &mut attributes);
20802090
if unused_template_params {
20812091
semantic_annotations.discards_template_param();
20822092
}
@@ -2144,7 +2154,10 @@ impl CodeGenerator for CompInfo {
21442154

21452155
if let Some(original_name) = item.original_name(ctx) {
21462156
if canonical_name != original_name {
2147-
let mut semantic_annotations = CppSemanticAttributeAdder::new(ctx.options(), &mut attributes);
2157+
let mut semantic_annotations = CppSemanticAttributeAdder::new(
2158+
ctx.options(),
2159+
&mut attributes,
2160+
);
21482161
semantic_annotations.original_name(&original_name);
21492162
}
21502163
}
@@ -3100,7 +3113,8 @@ impl CodeGenerator for Enum {
31003113

31013114
let mut attrs = vec![];
31023115

3103-
let mut semantic_annotations = CppSemanticAttributeAdder::new(ctx.options(), &mut attrs);
3116+
let mut semantic_annotations =
3117+
CppSemanticAttributeAdder::new(ctx.options(), &mut attrs);
31043118
if let Some(original_name) = item.original_name(ctx) {
31053119
if name != original_name {
31063120
semantic_annotations.original_name(&original_name);
@@ -3698,7 +3712,8 @@ impl RustTy {
36983712
inner: RustTyAnnotation,
36993713
) -> Self {
37003714
let annotation = match inner {
3701-
RustTyAnnotation::HasUnusedTemplateArgs | RustTyAnnotation::Opaque => inner,
3715+
RustTyAnnotation::HasUnusedTemplateArgs |
3716+
RustTyAnnotation::Opaque => inner,
37023717
_ => RustTyAnnotation::Reference,
37033718
};
37043719
Self { ts, annotation }
@@ -3709,7 +3724,8 @@ impl RustTy {
37093724
inner: RustTyAnnotation,
37103725
) -> Self {
37113726
let annotation = match inner {
3712-
RustTyAnnotation::HasUnusedTemplateArgs | RustTyAnnotation::Opaque => inner,
3727+
RustTyAnnotation::HasUnusedTemplateArgs |
3728+
RustTyAnnotation::Opaque => inner,
37133729
_ => RustTyAnnotation::RValueReference,
37143730
};
37153731
Self { ts, annotation }
@@ -3806,7 +3822,9 @@ impl TryToRustTy for Type {
38063822
IntKind::I8 => Ok(quote! { i8 }.into()),
38073823
IntKind::U8 => Ok(quote! { u8 }.into()),
38083824
IntKind::I16 => Ok(quote! { i16 }.into()),
3809-
IntKind::U16 if ctx.options().use_distinct_char16_t => Ok(quote! { c_char16_t }.into()),
3825+
IntKind::U16 if ctx.options().use_distinct_char16_t => {
3826+
Ok(quote! { c_char16_t }.into())
3827+
}
38103828
IntKind::U16 => Ok(quote! { u16 }.into()),
38113829
IntKind::I32 => Ok(quote! { i32 }.into()),
38123830
IntKind::U32 => Ok(quote! { u32 }.into()),
@@ -4103,7 +4121,7 @@ impl TryToRustTy for FunctionSig {
41034121
if !ctx.options().rust_features().vectorcall_abi =>
41044122
{
41054123
warn!("Skipping function with vectorcall ABI that isn't supported by the configured Rust target");
4106-
Ok(proc_macro2::TokenStream::new())
4124+
Ok(proc_macro2::TokenStream::new().into())
41074125
}
41084126
_ => Ok(quote! {
41094127
unsafe extern #abi fn ( #( #arguments ),* ) #ret
@@ -4136,22 +4154,25 @@ impl CodeGenerator for Function {
41364154
Linkage::External => {}
41374155
}
41384156

4139-
// Pure virtual methods have no actual symbol, so we can't generate
4140-
// something meaningful for them.
4141-
let is_dynamic_function = match self.kind() {
4142-
FunctionKind::Method(ref method_kind)
4143-
if method_kind.is_pure_virtual() =>
4144-
{
4145-
true
4157+
let is_pure_virtual = match self.kind() {
4158+
FunctionKind::Method(ref method_kind) => {
4159+
method_kind.is_pure_virtual()
41464160
}
4161+
_ => false,
4162+
};
4163+
4164+
let is_virtual = matches!(
4165+
self.kind(),
4166+
FunctionKind::Method(MethodKind::Virtual { .. })
4167+
);
4168+
4169+
let is_dynamic_function = match self.kind() {
41474170
FunctionKind::Function => {
41484171
ctx.options().dynamic_library_name.is_some()
41494172
}
41504173
_ => false,
41514174
};
41524175

4153-
let is_virtual = matches!(self.kind(), FunctionKind::Method(MethodKind::Virtual { .. }));
4154-
41554176
// Similar to static member variables in a class template, we can't
41564177
// generate bindings to template functions, because the set of
41574178
// instantiations is open ended and we have no way of knowing which
@@ -4207,7 +4228,8 @@ impl CodeGenerator for Function {
42074228
attributes.push(attributes::doc(comment));
42084229
}
42094230

4210-
let mut semantic_annotations = CppSemanticAttributeAdder::new(&ctx.options(), &mut attributes);
4231+
let mut semantic_annotations =
4232+
CppSemanticAttributeAdder::new(&ctx.options(), &mut attributes);
42114233

42124234
if is_pure_virtual {
42134235
semantic_annotations.is_pure_virtual();
@@ -4625,10 +4647,10 @@ pub(crate) fn codegen(
46254647
}
46264648

46274649
pub mod utils {
4628-
use super::{
4629-
error, RustTy, RustTyAnnotation, ToRustTyOrOpaque,
4650+
use super::{error, RustTy, RustTyAnnotation, ToRustTyOrOpaque};
4651+
use crate::codegen::helpers::{
4652+
CppSemanticAttributeCreator, CppSemanticAttributeSingle,
46304653
};
4631-
use crate::codegen::helpers::{CppSemanticAttributeSingle, CppSemanticAttributeCreator};
46324654
use crate::ir::context::BindgenContext;
46334655
use crate::ir::function::{Abi, FunctionSig};
46344656
use crate::ir::item::{Item, ItemCanonicalPath};
@@ -4958,23 +4980,21 @@ pub mod utils {
49584980
let ts = quote! {
49594981
-> #ret_ty
49604982
};
4961-
let mut semantic_annotation = CppSemanticAttributeSingle::new(ctx.options());
4983+
let mut semantic_annotation =
4984+
CppSemanticAttributeSingle::new(ctx.options());
49624985
match annotations {
4963-
super::RustTyAnnotation::None => {},
4986+
super::RustTyAnnotation::None => {}
49644987
super::RustTyAnnotation::Reference => {
49654988
semantic_annotation.ret_type_reference()
49664989
}
49674990
super::RustTyAnnotation::RValueReference => {
49684991
semantic_annotation.ret_type_rvalue_reference()
49694992
}
4970-
super::RustTyAnnotation::HasUnusedTemplateArgs | super::RustTyAnnotation::Opaque => {
4971-
semantic_annotation.incomprehensible_param_in_arg_or_return()
4972-
}
4993+
super::RustTyAnnotation::HasUnusedTemplateArgs |
4994+
super::RustTyAnnotation::Opaque => semantic_annotation
4995+
.incomprehensible_param_in_arg_or_return(),
49734996
};
4974-
(
4975-
ts,
4976-
semantic_annotation.result()
4977-
)
4997+
(ts, semantic_annotation.result())
49784998
}
49794999
}
49805000

@@ -5042,25 +5062,26 @@ pub mod utils {
50425062

50435063
assert!(!arg_name.is_empty());
50445064
let arg_name = ctx.rust_ident(arg_name);
5045-
let mut semantic_annotation = CppSemanticAttributeSingle::new(ctx.options());
5065+
let mut semantic_annotation =
5066+
CppSemanticAttributeSingle::new(ctx.options());
50465067
match arg_details.annotation {
5047-
RustTyAnnotation::None => {},
5068+
RustTyAnnotation::None => {}
50485069
RustTyAnnotation::Reference => {
50495070
semantic_annotation.arg_type_reference(&arg_name)
50505071
}
50515072
RustTyAnnotation::RValueReference => {
50525073
semantic_annotation.arg_type_rvalue_reference(&arg_name)
50535074
}
5054-
RustTyAnnotation::HasUnusedTemplateArgs | RustTyAnnotation::Opaque => {
5055-
semantic_annotation.incomprehensible_param_in_arg_or_return()
5056-
}
5075+
RustTyAnnotation::HasUnusedTemplateArgs |
5076+
RustTyAnnotation::Opaque => semantic_annotation
5077+
.incomprehensible_param_in_arg_or_return(),
50575078
};
50585079

50595080
(
50605081
quote! {
50615082
#arg_name : #arg_ty
50625083
},
5063-
semantic_annotation.result()
5084+
semantic_annotation.result(),
50645085
)
50655086
})
50665087
.unzip();

src/ir/enum_ty.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,16 @@ pub struct Enum {
4141

4242
impl Enum {
4343
/// Construct a new `Enum` with the given representation and variants.
44-
pub fn new(repr: Option<TypeId>, variants: Vec<EnumVariant>, visibility: Visibility) -> Self {
45-
Enum { repr, variants, visibility }
44+
pub fn new(
45+
repr: Option<TypeId>,
46+
variants: Vec<EnumVariant>,
47+
visibility: Visibility,
48+
) -> Self {
49+
Enum {
50+
repr,
51+
variants,
52+
visibility,
53+
}
4654
}
4755

4856
/// Get this enumeration's representation.

0 commit comments

Comments
 (0)