Skip to content

Commit

Permalink
Metadata parsing and generation (#2525)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored May 30, 2023
1 parent 72e9bf4 commit 593ff2e
Show file tree
Hide file tree
Showing 33 changed files with 542 additions and 377 deletions.
1 change: 1 addition & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ jobs:
cargo clippy -p test_reserved &&
cargo clippy -p test_resources &&
cargo clippy -p test_return_struct &&
cargo clippy -p test_riddle &&
cargo clippy -p test_simple_component &&
cargo clippy -p test_standalone &&
cargo clippy -p test_string_param &&
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ jobs:
cargo test -p test_enums &&
cargo test -p test_error &&
cargo test -p test_event &&
cargo clean &&
cargo test -p test_extensions &&
cargo clean &&
cargo test -p test_handles &&
cargo test -p test_helpers &&
cargo test -p test_implement &&
Expand All @@ -116,6 +116,7 @@ jobs:
cargo test -p test_reserved &&
cargo test -p test_resources &&
cargo test -p test_return_struct &&
cargo test -p test_riddle &&
cargo test -p test_simple_component &&
cargo test -p test_standalone &&
cargo test -p test_string_param &&
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ bin
*.user
*.filters
*.bin
*.idl
*.winmd
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 @@ -220,15 +220,15 @@ fn gen_win32_invoke_arg(gen: &Gen, param: &SignatureParam) -> TokenStream {
if gen
.reader
.param_flags(param.def)
.contains(ParamAttributes::INPUT)
.contains(ParamAttributes::In)
&& 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)
.contains(ParamAttributes::INPUT)
.contains(ParamAttributes::In)
&& !gen.reader.type_is_primitive(&param.ty))
{
quote! { ::core::mem::transmute(&#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
Expand Up @@ -4,7 +4,7 @@ pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {
if gen
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
gen_delegate(gen, def)
} else {
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 @@ -17,7 +17,7 @@ pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {
if gen
.reader
.field_flags(field)
.contains(FieldAttributes::LITERAL)
.contains(FieldAttributes::Literal)
{
let field_name = to_ident(gen.reader.field_name(field));
let constant = gen.reader.field_constant(field).unwrap();
Expand Down Expand Up @@ -185,7 +185,7 @@ pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {
if gen
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
let signature =
Literal::byte_string(gen.reader.type_def_signature(def, &[]).as_bytes());
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 @@ -270,7 +270,7 @@ fn handle_last_error(gen: &Gen, def: MethodDef, signature: &Signature) -> bool {
if gen
.reader
.impl_map_flags(map)
.contains(PInvokeAttributes::LAST_ERROR)
.contains(PInvokeAttributes::SupportsLastError)
{
if let Type::TypeDef((return_type, _)) = &signature.return_type {
if gen.reader.type_def_is_handle(*return_type) {
Expand Down
38 changes: 17 additions & 21 deletions crates/libs/bindgen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl<'a> Gen<'a> {
if self
.reader
.type_def_flags(*def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
quote! { *mut ::core::ffi::c_void }
} else {
Expand Down Expand Up @@ -692,7 +692,7 @@ impl<'a> Gen<'a> {
if self
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
let type_signature = if self.reader.type_def_kind(def) == TypeKind::Class {
let type_signature =
Expand Down Expand Up @@ -756,7 +756,7 @@ impl<'a> Gen<'a> {
if self
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
// 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 @@ -918,7 +918,7 @@ impl<'a> Gen<'a> {
let is_winrt = self
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT);
.contains(TypeAttributes::WindowsRuntime);

let crate_name = self.crate_name();

Expand Down Expand Up @@ -958,11 +958,7 @@ impl<'a> Gen<'a> {
let abi_size_name: TokenStream =
format!("{}_array_size", self.reader.param_name(p.def)).into();

if self
.reader
.param_flags(p.def)
.contains(ParamAttributes::INPUT)
{
if self.reader.param_flags(p.def).contains(ParamAttributes::In) {
if p.ty.is_winrt_array() {
quote! { #abi_size_name: u32, #name: *const #abi, }
} else if p.ty.is_const_ref() {
Expand Down Expand Up @@ -1036,7 +1032,7 @@ impl<'a> Gen<'a> {
SignatureParamKind::ArrayFixed(_)
| SignatureParamKind::ArrayRelativeLen(_)
| SignatureParamKind::ArrayRelativeByteLen(_) => {
let map = if flags.contains(ParamAttributes::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 @@ -1046,7 +1042,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.contains(ParamAttributes::OPTIONAL) {
if flags.contains(ParamAttributes::Optional) {
quote! { #name.as_deref().map_or(0, |slice|slice.len() as _), }
} else {
quote! { #name.len() as _, }
Expand All @@ -1059,7 +1055,7 @@ impl<'a> Gen<'a> {
quote! { #name.into_param().abi(), }
}
SignatureParamKind::OptionalPointer => {
if flags.contains(ParamAttributes::OUTPUT) {
if flags.contains(ParamAttributes::Out) {
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 @@ -1111,7 +1107,7 @@ impl<'a> Gen<'a> {
let ty = if self
.reader
.param_flags(param.def)
.contains(ParamAttributes::OUTPUT)
.contains(ParamAttributes::Out)
{
quote! { &mut [#ty; #len] }
} else {
Expand All @@ -1120,7 +1116,7 @@ impl<'a> Gen<'a> {
if self
.reader
.param_flags(param.def)
.contains(ParamAttributes::OPTIONAL)
.contains(ParamAttributes::Optional)
{
tokens.combine(&quote! { #name: ::core::option::Option<#ty>, });
} else {
Expand All @@ -1133,7 +1129,7 @@ impl<'a> Gen<'a> {
let ty = if self
.reader
.param_flags(param.def)
.contains(ParamAttributes::OUTPUT)
.contains(ParamAttributes::Out)
{
quote! { &mut [#ty] }
} else {
Expand All @@ -1142,7 +1138,7 @@ impl<'a> Gen<'a> {
if self
.reader
.param_flags(param.def)
.contains(ParamAttributes::OPTIONAL)
.contains(ParamAttributes::Optional)
{
tokens.combine(&quote! { #name: ::core::option::Option<#ty>, });
} else {
Expand All @@ -1153,7 +1149,7 @@ impl<'a> Gen<'a> {
let ty = if self
.reader
.param_flags(param.def)
.contains(ParamAttributes::OUTPUT)
.contains(ParamAttributes::Out)
{
quote! { &mut [u8] }
} else {
Expand All @@ -1162,7 +1158,7 @@ impl<'a> Gen<'a> {
if self
.reader
.param_flags(param.def)
.contains(ParamAttributes::OPTIONAL)
.contains(ParamAttributes::Optional)
{
tokens.combine(&quote! { #name: ::core::option::Option<#ty>, });
} else {
Expand Down Expand Up @@ -1197,7 +1193,7 @@ impl<'a> Gen<'a> {
if self
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
let is_delegate = self.reader.type_def_kind(def) == TypeKind::Delegate;
let params = signature
Expand Down Expand Up @@ -1262,7 +1258,7 @@ impl<'a> Gen<'a> {
let sig = if self
.reader
.param_flags(param.def)
.contains(ParamAttributes::INPUT)
.contains(ParamAttributes::In)
{
if param.ty.is_winrt_array() {
quote! { &[#default_type] }
Expand Down Expand Up @@ -1297,7 +1293,7 @@ impl<'a> Gen<'a> {
if self
.reader
.param_flags(param.def)
.contains(ParamAttributes::INPUT)
.contains(ParamAttributes::In)
{
if self.reader.type_is_primitive(&param.ty) {
quote! { #name: #kind, }
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 @@ -49,7 +49,7 @@ pub fn gen(gen: &Gen, def: TypeDef) -> TokenStream {
if gen
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
// TODO: this awkward wrapping of TypeDefs needs fixing
for interface in gen
Expand Down Expand Up @@ -82,7 +82,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).contains(TypeAttributes::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::WindowsRuntime) { 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 @@ -66,7 +66,7 @@ fn gen_win_interface(gen: &Gen, def: TypeDef) -> TokenStream {
if gen
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
for method in gen.reader.type_def_methods(def) {
methods.combine(&winrt_methods::gen(
Expand Down Expand Up @@ -170,7 +170,7 @@ fn gen_win_interface(gen: &Gen, def: TypeDef) -> TokenStream {
if gen
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
for interface in &interfaces {
let into = gen.type_name(&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 @@ -87,7 +87,7 @@ pub fn namespace(gen: &Gen, tree: &Tree) -> String {
if gen
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
types
.entry(kind)
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 @@ -50,16 +50,16 @@ 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).contains(FieldAttributes::LITERAL) {
if gen.reader.field_flags(f).contains(FieldAttributes::Literal) {
quote! {}
} else if !gen.sys
&& flags.contains(TypeAttributes::EXPLICIT_LAYOUT)
&& flags.contains(TypeAttributes::ExplicitLayout)
&& !gen.reader.field_is_copyable(f, def)
{
let ty = gen.type_default_name(&ty);
quote! { pub #name: ::std::mem::ManuallyDrop<#ty>, }
} else if !gen.sys
&& !flags.contains(TypeAttributes::WINRT)
&& !flags.contains(TypeAttributes::WindowsRuntime)
&& !gen.reader.field_is_blittable(f, def)
{
if let Type::Win32Array((ty, len)) = ty {
Expand All @@ -75,7 +75,7 @@ fn gen_struct_with_name(gen: &Gen, def: TypeDef, struct_name: &str, cfg: &Cfg) -
}
});

let struct_or_union = if flags.contains(TypeAttributes::EXPLICIT_LAYOUT) {
let struct_or_union = if flags.contains(TypeAttributes::ExplicitLayout) {
quote! { union }
} else {
quote! { struct }
Expand Down Expand Up @@ -139,7 +139,7 @@ fn gen_windows_traits(gen: &Gen, def: TypeDef, name: &TokenStream, cfg: &Cfg) ->
if gen
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
let signature =
Literal::byte_string(gen.reader.type_def_signature(def, &[]).as_bytes());
Expand Down Expand Up @@ -168,7 +168,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).contains(FieldAttributes::LITERAL) {
if gen.reader.field_flags(f).contains(FieldAttributes::Literal) {
None
} else {
Some(quote! { self.#name == other.#name })
Expand Down Expand Up @@ -199,7 +199,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).contains(FieldAttributes::LITERAL) {
if gen.reader.field_flags(f).contains(FieldAttributes::Literal) {
None
} else {
let name = gen.reader.field_name(f);
Expand Down Expand Up @@ -244,7 +244,7 @@ fn gen_copy_clone(gen: &Gen, def: TypeDef, name: &TokenStream, cfg: &Cfg) -> Tok
} else if !gen
.reader
.type_def_flags(def)
.contains(TypeAttributes::WINRT)
.contains(TypeAttributes::WindowsRuntime)
{
quote! {
#features
Expand All @@ -257,7 +257,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).contains(FieldAttributes::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 Down Expand Up @@ -286,7 +286,7 @@ fn gen_struct_constants(
let features = gen.cfg_features(cfg);

let constants = gen.reader.type_def_fields(def).filter_map(|f| {
if gen.reader.field_flags(f).contains(FieldAttributes::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
6 changes: 3 additions & 3 deletions crates/libs/bindgen/src/winrt_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn gen_winrt_params(gen: &Gen, params: &[SignatureParam]) -> TokenStream {
if gen
.reader
.param_flags(param.def)
.contains(ParamAttributes::INPUT)
.contains(ParamAttributes::In)
{
if param.ty.is_winrt_array() {
result.combine(&quote! { #name: &[#default_type], });
Expand Down Expand Up @@ -152,7 +152,7 @@ fn gen_winrt_abi_args(gen: &Gen, params: &[SignatureParam]) -> TokenStream {
let param = if gen
.reader
.param_flags(param.def)
.contains(ParamAttributes::INPUT)
.contains(ParamAttributes::In)
{
if param.ty.is_winrt_array() {
if gen.reader.type_is_blittable(&param.ty) {
Expand Down Expand Up @@ -245,7 +245,7 @@ fn gen_winrt_invoke_arg(gen: &Gen, param: &SignatureParam) -> TokenStream {
if gen
.reader
.param_flags(param.def)
.contains(ParamAttributes::INPUT)
.contains(ParamAttributes::In)
{
if param.ty.is_winrt_array() {
quote! { ::core::slice::from_raw_parts(::core::mem::transmute_copy(&#name), #abi_size_name as _) }
Expand Down
Loading

0 comments on commit 593ff2e

Please sign in to comment.