Skip to content

Commit

Permalink
Metadata writer refactoring (#2309)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jan 26, 2023
1 parent 15cefaf commit 1685363
Show file tree
Hide file tree
Showing 53 changed files with 1,227 additions and 1,118 deletions.
1 change: 0 additions & 1 deletion .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ jobs:
cargo clippy -p test_window_long &&
cargo clippy -p test_winrt &&
cargo clippy -p tool_gnu &&
cargo clippy -p tool_ilrs &&
cargo clippy -p tool_lib &&
cargo clippy -p tool_license &&
cargo clippy -p tool_msvc &&
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ jobs:
cargo test --target ${{ matrix.target }} -p test_enums &&
cargo test --target ${{ matrix.target }} -p test_error &&
cargo test --target ${{ matrix.target }} -p test_event &&
cargo test --target ${{ matrix.target }} -p test_extensions &&
cargo clean &&
cargo test --target ${{ matrix.target }} -p test_extensions &&
cargo test --target ${{ matrix.target }} -p test_handles &&
cargo test --target ${{ matrix.target }} -p test_helpers &&
cargo test --target ${{ matrix.target }} -p test_implement &&
Expand Down Expand Up @@ -163,7 +163,6 @@ jobs:
cargo test --target ${{ matrix.target }} -p test_window_long &&
cargo test --target ${{ matrix.target }} -p test_winrt &&
cargo test --target ${{ matrix.target }} -p tool_gnu &&
cargo test --target ${{ matrix.target }} -p tool_ilrs &&
cargo test --target ${{ matrix.target }} -p tool_lib &&
cargo test --target ${{ matrix.target }} -p tool_license &&
cargo test --target ${{ matrix.target }} -p tool_msvc &&
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/com_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ pub fn gen_upcall(gen: &Gen, sig: &Signature, inner: TokenStream) -> TokenStream
fn gen_win32_invoke_arg(gen: &Gen, param: &SignatureParam) -> TokenStream {
let name = gen.param_name(param.def);

if gen.reader.param_flags(param.def).input() && gen.reader.type_is_nullable(&param.ty) {
if gen.reader.param_flags(param.def).contains(ParamAttributes::INPUT) && gen.reader.type_is_nullable(&param.ty) {
quote! { ::windows::core::from_raw_borrowed(&#name) }
} else if (!param.ty.is_pointer() && gen.reader.type_is_nullable(&param.ty)) || (gen.reader.param_flags(param.def).input() && !gen.reader.type_is_primitive(&param.ty)) {
} else if (!param.ty.is_pointer() && gen.reader.type_is_nullable(&param.ty)) || (gen.reader.param_flags(param.def).contains(ParamAttributes::INPUT) && !gen.reader.type_is_primitive(&param.ty)) {
quote! { ::core::mem::transmute(&#name) }
} else {
quote! { ::core::mem::transmute_copy(&#name) }
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/delegates.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {
if gen.reader.type_def_flags(def).winrt() {
if gen.reader.type_def_flags(def).contains(TypeAttributes::WINRT) {
gen_delegate(gen, def)
} else {
gen_callback(gen, def)
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {
.reader
.type_def_fields(def)
.filter_map(|field| {
if gen.reader.field_flags(field).literal() {
if gen.reader.field_flags(field).contains(FieldAttributes::LITERAL) {
let field_name = to_ident(gen.reader.field_name(field));
let constant = gen.reader.field_constant(field).unwrap();
let value = gen.value(&gen.reader.constant_value(constant));
Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {
});
}

if gen.reader.type_def_flags(def).winrt() {
if gen.reader.type_def_flags(def).contains(TypeAttributes::WINRT) {
let signature = Literal::byte_string(gen.reader.type_def_signature(def, &[]).as_bytes());

tokens.combine(&quote! {
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ fn does_not_return(gen: &Gen, def: MethodDef) -> TokenStream {

fn handle_last_error(gen: &Gen, def: MethodDef, signature: &Signature) -> bool {
if let Some(map) = gen.reader.method_def_impl_map(def) {
if gen.reader.impl_map_flags(map).last_error() {
if gen.reader.impl_map_flags(map).contains(PInvokeAttributes::LAST_ERROR) {
if let Some(Type::TypeDef((return_type, _))) = &signature.return_type {
if gen.reader.type_def_is_handle(*return_type) {
if gen.reader.type_def_underlying_type(*return_type).is_pointer() {
Expand Down
34 changes: 17 additions & 17 deletions crates/libs/bindgen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<'a> Gen<'a> {
}
}
TypeKind::Delegate => {
if self.reader.type_def_flags(*def).winrt() {
if self.reader.type_def_flags(*def).contains(TypeAttributes::WINRT) {
quote! { *mut ::core::ffi::c_void }
} else {
self.type_def_name(*def, &[])
Expand Down Expand Up @@ -619,7 +619,7 @@ impl<'a> Gen<'a> {
}
}
pub fn interface_winrt_trait(&self, def: TypeDef, generics: &[Type], ident: &TokenStream, constraints: &TokenStream, _phantoms: &TokenStream, features: &TokenStream) -> TokenStream {
if self.reader.type_def_flags(def).winrt() {
if self.reader.type_def_flags(def).contains(TypeAttributes::WINRT) {
let type_signature = if self.reader.type_def_kind(def) == TypeKind::Class {
let type_signature = Literal::byte_string(self.reader.type_def_signature(def, generics).as_bytes());
quote! { ::windows::core::ConstBuffer::from_slice(#type_signature) }
Expand Down Expand Up @@ -673,7 +673,7 @@ impl<'a> Gen<'a> {
}
}
pub fn runtime_name_trait(&self, def: TypeDef, _generics: &[Type], name: &TokenStream, constraints: &TokenStream, features: &TokenStream) -> TokenStream {
if self.reader.type_def_flags(def).winrt() {
if self.reader.type_def_flags(def).contains(TypeAttributes::WINRT) {
// TODO: this needs to use a ConstBuffer-like facility to accomodate generics
let runtime_name = format!("{}", self.reader.type_def_type_name(def));

Expand Down Expand Up @@ -793,7 +793,7 @@ impl<'a> Gen<'a> {
}
}
pub fn vtbl_signature(&self, def: TypeDef, _generics: &[Type], signature: &Signature) -> TokenStream {
let is_winrt = self.reader.type_def_flags(def).winrt();
let is_winrt = self.reader.type_def_flags(def).contains(TypeAttributes::WINRT);
let hresult = self.type_name(&Type::HRESULT);

let (trailing_return_type, return_type, udt_return_type) = if is_winrt {
Expand Down Expand Up @@ -826,7 +826,7 @@ impl<'a> Gen<'a> {
let abi = self.type_abi_name(&p.ty);
let abi_size_name: TokenStream = format!("{}_array_size", self.reader.param_name(p.def)).into();

if self.reader.param_flags(p.def).input() {
if self.reader.param_flags(p.def).contains(ParamAttributes::INPUT) {
if p.ty.is_winrt_array() {
quote! { #abi_size_name: u32, #name: *const #abi, }
} else if p.ty.is_winrt_const_ref() {
Expand Down Expand Up @@ -894,7 +894,7 @@ impl<'a> Gen<'a> {
let flags = self.reader.param_flags(param.def);
match param.kind {
SignatureParamKind::ArrayFixed(_) | SignatureParamKind::ArrayRelativeLen(_) | SignatureParamKind::ArrayRelativeByteLen(_) => {
let map = if flags.optional() {
let map = if flags.contains(ParamAttributes::OPTIONAL) {
quote! { #name.as_deref().map_or(::core::ptr::null(), |slice|slice.as_ptr()) }
} else {
quote! { #name.as_ptr() }
Expand All @@ -904,7 +904,7 @@ impl<'a> Gen<'a> {
SignatureParamKind::ArrayRelativePtr(relative) => {
let name = self.param_name(params[relative].def);
let flags = self.reader.param_flags(params[relative].def);
if flags.optional() {
if flags.contains(ParamAttributes::OPTIONAL) {
quote! { #name.as_deref().map_or(0, |slice|slice.len() as _), }
} else {
quote! { #name.len() as _, }
Expand All @@ -920,7 +920,7 @@ impl<'a> Gen<'a> {
quote! { #name.into(), }
}
SignatureParamKind::OptionalPointer => {
if flags.output() {
if flags.contains(ParamAttributes::OUTPUT) {
quote! { ::core::mem::transmute(#name.unwrap_or(::std::ptr::null_mut())), }
} else {
quote! { ::core::mem::transmute(#name.unwrap_or(::std::ptr::null())), }
Expand Down Expand Up @@ -967,12 +967,12 @@ impl<'a> Gen<'a> {
let ty = param.ty.deref();
let ty = self.type_default_name(&ty);
let len = Literal::u32_unsuffixed(fixed as _);
let ty = if self.reader.param_flags(param.def).output() {
let ty = if self.reader.param_flags(param.def).contains(ParamAttributes::OUTPUT) {
quote! { &mut [#ty; #len] }
} else {
quote! { &[#ty; #len] }
};
if self.reader.param_flags(param.def).optional() {
if self.reader.param_flags(param.def).contains(ParamAttributes::OPTIONAL) {
tokens.combine(&quote! { #name: ::core::option::Option<#ty>, });
} else {
tokens.combine(&quote! { #name: #ty, });
Expand All @@ -981,24 +981,24 @@ impl<'a> Gen<'a> {
SignatureParamKind::ArrayRelativeLen(_) => {
let ty = param.ty.deref();
let ty = self.type_default_name(&ty);
let ty = if self.reader.param_flags(param.def).output() {
let ty = if self.reader.param_flags(param.def).contains(ParamAttributes::OUTPUT) {
quote! { &mut [#ty] }
} else {
quote! { &[#ty] }
};
if self.reader.param_flags(param.def).optional() {
if self.reader.param_flags(param.def).contains(ParamAttributes::OPTIONAL) {
tokens.combine(&quote! { #name: ::core::option::Option<#ty>, });
} else {
tokens.combine(&quote! { #name: #ty, });
}
}
SignatureParamKind::ArrayRelativeByteLen(_) => {
let ty = if self.reader.param_flags(param.def).output() {
let ty = if self.reader.param_flags(param.def).contains(ParamAttributes::OUTPUT) {
quote! { &mut [u8] }
} else {
quote! { &[u8] }
};
if self.reader.param_flags(param.def).optional() {
if self.reader.param_flags(param.def).contains(ParamAttributes::OPTIONAL) {
tokens.combine(&quote! { #name: ::core::option::Option<#ty>, });
} else {
tokens.combine(&quote! { #name: #ty, });
Expand Down Expand Up @@ -1029,7 +1029,7 @@ impl<'a> Gen<'a> {
}

pub fn impl_signature(&self, def: TypeDef, signature: &Signature) -> TokenStream {
if self.reader.type_def_flags(def).winrt() {
if self.reader.type_def_flags(def).contains(TypeAttributes::WINRT) {
let is_delegate = self.reader.type_def_kind(def) == TypeKind::Delegate;
let params = signature.params.iter().map(|p| self.winrt_produce_type(p, !is_delegate));

Expand Down Expand Up @@ -1084,7 +1084,7 @@ impl<'a> Gen<'a> {
fn winrt_produce_type(&self, param: &SignatureParam, include_param_names: bool) -> TokenStream {
let default_type = self.type_default_name(&param.ty);

let sig = if self.reader.param_flags(param.def).input() {
let sig = if self.reader.param_flags(param.def).contains(ParamAttributes::INPUT) {
if param.ty.is_winrt_array() {
quote! { &[#default_type] }
} else if self.reader.type_is_primitive(&param.ty) {
Expand Down Expand Up @@ -1115,7 +1115,7 @@ impl<'a> Gen<'a> {
let name = self.param_name(param.def);
let kind = self.type_default_name(&param.ty);

if self.reader.param_flags(param.def).input() {
if self.reader.param_flags(param.def).contains(ParamAttributes::INPUT) {
if self.reader.type_is_primitive(&param.ty) {
quote! { #name: #kind, }
} else if self.reader.type_is_nullable(&param.ty) {
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/implements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {
}
}

if gen.reader.type_def_flags(def).winrt() {
if gen.reader.type_def_flags(def).contains(TypeAttributes::WINRT) {
// TODO: this awkward wrapping of TypeDefs needs fixing
for interface in gen.reader.type_interfaces(&Type::TypeDef((def, generics.to_vec()))) {
if let Type::TypeDef((def, generics)) = interface.ty {
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {
let signature = gen.reader.method_def_signature(method, generics);
let vtbl_signature = gen.vtbl_signature(def, generics, &signature);

let invoke_upcall = if gen.reader.type_def_flags(def).winrt() { winrt_methods::gen_upcall(gen, &signature, quote! { this.#name }) } else { com_methods::gen_upcall(gen, &signature, quote! { this.#name }) };
let invoke_upcall = if gen.reader.type_def_flags(def).contains(TypeAttributes::WINRT) { winrt_methods::gen_upcall(gen, &signature, quote! { this.#name }) } else { com_methods::gen_upcall(gen, &signature, quote! { this.#name }) };

if has_unknown_base {
quote! {
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn gen_win_interface(gen: &Gen, def: TypeDef) -> TokenStream {
let method_names = &mut MethodNames::new();
let virtual_names = &mut MethodNames::new();

if gen.reader.type_def_flags(def).winrt() {
if gen.reader.type_def_flags(def).contains(TypeAttributes::WINRT) {
for method in gen.reader.type_def_methods(def) {
methods.combine(&winrt_methods::gen(gen, def, generics, InterfaceKind::Default, method, method_names, virtual_names));
}
Expand Down Expand Up @@ -141,7 +141,7 @@ fn gen_win_interface(gen: &Gen, def: TypeDef) -> TokenStream {
}
}

if gen.reader.type_def_flags(def).winrt() {
if gen.reader.type_def_flags(def).contains(TypeAttributes::WINRT) {
for interface in &interfaces {
let into = gen.type_name(&interface.ty);
let cfg = gen.cfg_features(&cfg.union(&gen.reader.type_cfg(&interface.ty)));
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn namespace(gen: &Gen, tree: &Tree) -> String {
let kind = gen.reader.type_def_kind(def);
match kind {
TypeKind::Class => {
if gen.reader.type_def_flags(def).winrt() {
if gen.reader.type_def_flags(def).contains(TypeAttributes::WINRT) {
types.entry(kind).or_default().insert(name, classes::gen(gen, def));
} else {
for method in gen.reader.type_def_methods(def) {
Expand Down
20 changes: 10 additions & 10 deletions crates/libs/bindgen/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ fn gen_struct_with_name(gen: &Gen, def: TypeDef, struct_name: &str, cfg: &Cfg) -
let name = to_ident(gen.reader.field_name(f));
let ty = gen.reader.field_type(f, Some(def));

if gen.reader.field_flags(f).literal() {
if gen.reader.field_flags(f).contains(FieldAttributes::LITERAL) {
quote! {}
} else if !gen.sys && flags.explicit_layout() && !gen.reader.field_is_copyable(f, def) {
} else if !gen.sys && flags.contains(TypeAttributes::EXPLICIT_LAYOUT) && !gen.reader.field_is_copyable(f, def) {
// Rust can't tell that the type is copyable and won't accept windows::core::ManuallyDrop
let ty = gen.type_default_name(&ty);
quote! { pub #name: ::std::mem::ManuallyDrop<#ty>, }
} else if !gen.sys && !flags.winrt() && !gen.reader.field_is_blittable(f, def) {
} else if !gen.sys && !flags.contains(TypeAttributes::WINRT) && !gen.reader.field_is_blittable(f, def) {
if let Type::Win32Array((ty, len)) = ty {
let ty = gen.type_name(&ty);
quote! { pub #name: [::windows::core::ManuallyDrop<#ty>; #len], }
Expand All @@ -56,7 +56,7 @@ fn gen_struct_with_name(gen: &Gen, def: TypeDef, struct_name: &str, cfg: &Cfg) -
}
});

let struct_or_union = if flags.explicit_layout() {
let struct_or_union = if flags.contains(TypeAttributes::EXPLICIT_LAYOUT) {
quote! { union }
} else {
quote! { struct }
Expand Down Expand Up @@ -118,7 +118,7 @@ fn gen_windows_traits(gen: &Gen, def: TypeDef, name: &TokenStream, cfg: &Cfg) ->
}
};

if gen.reader.type_def_flags(def).winrt() {
if gen.reader.type_def_flags(def).contains(TypeAttributes::WINRT) {
let signature = Literal::byte_string(gen.reader.type_def_signature(def, &[]).as_bytes());

let clone = if gen.reader.type_def_is_blittable(def) {
Expand Down Expand Up @@ -151,7 +151,7 @@ fn gen_compare_traits(gen: &Gen, def: TypeDef, name: &TokenStream, cfg: &Cfg) ->
} else {
let fields = gen.reader.type_def_fields(def).filter_map(|f| {
let name = to_ident(gen.reader.field_name(f));
if gen.reader.field_flags(f).literal() {
if gen.reader.field_flags(f).contains(FieldAttributes::LITERAL) {
None
} else {
Some(quote! { self.#name == other.#name })
Expand Down Expand Up @@ -179,7 +179,7 @@ fn gen_debug(gen: &Gen, def: TypeDef, ident: &TokenStream, cfg: &Cfg) -> TokenSt
let features = gen.cfg_features(cfg);

let fields = gen.reader.type_def_fields(def).filter_map(|f| {
if gen.reader.field_flags(f).literal() {
if gen.reader.field_flags(f).contains(FieldAttributes::LITERAL) {
None
} else {
let name = gen.reader.field_name(f);
Expand Down Expand Up @@ -221,7 +221,7 @@ fn gen_copy_clone(gen: &Gen, def: TypeDef, name: &TokenStream, cfg: &Cfg) -> Tok
} else if gen.reader.type_def_class_layout(def).is_some() {
// Don't support copy/clone of packed structs: https://github.com/rust-lang/rust/issues/82523
quote! {}
} else if !gen.reader.type_def_flags(def).winrt() {
} else if !gen.reader.type_def_flags(def).contains(TypeAttributes::WINRT) {
quote! {
#features
impl ::core::clone::Clone for #name {
Expand All @@ -233,7 +233,7 @@ fn gen_copy_clone(gen: &Gen, def: TypeDef, name: &TokenStream, cfg: &Cfg) -> Tok
} else {
let fields = gen.reader.type_def_fields(def).map(|f| {
let name = to_ident(gen.reader.field_name(f));
if gen.reader.field_flags(f).literal() {
if gen.reader.field_flags(f).contains(FieldAttributes::LITERAL) {
quote! {}
} else if gen.reader.field_is_blittable(f, def) {
quote! { #name: self.#name }
Expand All @@ -257,7 +257,7 @@ fn gen_struct_constants(gen: &Gen, def: TypeDef, struct_name: &TokenStream, cfg:
let features = gen.cfg_features(cfg);

let constants = gen.reader.type_def_fields(def).filter_map(|f| {
if gen.reader.field_flags(f).literal() {
if gen.reader.field_flags(f).contains(FieldAttributes::LITERAL) {
if let Some(constant) = gen.reader.field_constant(f) {
let name = to_ident(gen.reader.field_name(f));
let value = gen.typed_value(&gen.reader.constant_value(constant));
Expand Down
Loading

0 comments on commit 1685363

Please sign in to comment.