Skip to content
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

Update to riddle and metadata generation #2510

Merged
merged 5 commits into from
May 18, 2023
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
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
rustflags = [
# "--cfg", "windows_debugger_visualizer",
# "--cfg", "windows_raw_dylib",
# "-C", "target-feature=+crt-static",
]
1 change: 1 addition & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ jobs:
cargo clippy -p tool_gnu &&
cargo clippy -p tool_lib &&
cargo clippy -p tool_license &&
cargo clippy -p tool_metadata &&
cargo clippy -p tool_msvc &&
cargo clippy -p tool_sys &&
cargo clippy -p tool_windows &&
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/gen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: windows-latest
strategy:
matrix:
tool: [windows, sys, yml, license, core]
tool: [windows, sys, yml, license, core, metadata]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ jobs:
cargo test -p tool_gnu &&
cargo test -p tool_lib &&
cargo test -p tool_license &&
cargo test -p tool_metadata &&
cargo test -p tool_msvc &&
cargo test -p tool_sys &&
cargo test -p tool_windows &&
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ bin
*.user
*.filters
*.bin
*.idl
*.winmd
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/com_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ pub fn gen(
SignatureKind::ReturnStruct => {
let args = gen.win32_args(&signature.params, kind);
let params = gen.win32_params(&signature.params, kind);
let return_type = gen.type_name(&signature.return_type.unwrap());
let return_type = gen.type_name(&signature.return_type);

quote! {
#doc
Expand Down
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fn gen_win_function(gen: &Gen, def: MethodDef) -> TokenStream {
if handle_last_error(gen, def, &signature) {
let args = gen.win32_args(&signature.params, kind);
let params = gen.win32_params(&signature.params, kind);
let return_type = gen.type_name(&signature.return_type.unwrap());
let return_type = gen.type_name(&signature.return_type);

quote! {
#doc
Expand Down Expand Up @@ -272,7 +272,7 @@ fn handle_last_error(gen: &Gen, def: MethodDef, signature: &Signature) -> bool {
.impl_map_flags(map)
.contains(PInvokeAttributes::LAST_ERROR)
{
if let Some(Type::TypeDef((return_type, _))) = &signature.return_type {
if let Type::TypeDef((return_type, _)) = &signature.return_type {
if gen.reader.type_def_is_handle(*return_type) {
if gen
.reader
Expand Down
89 changes: 44 additions & 45 deletions crates/libs/bindgen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'a> Gen<'a> {
}
Type::WinrtArray(ty) => self.type_name(ty),
Type::WinrtArrayRef(ty) => self.type_name(ty),
Type::WinrtConstRef(ty) => self.type_name(ty),
Type::ConstRef(ty) => self.type_name(ty),
_ => unimplemented!(),
}
}
Expand Down Expand Up @@ -919,38 +919,36 @@ impl<'a> Gen<'a> {
.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 {
if let Some(return_type) = &signature.return_type {
if let Type::WinrtArray(kind) = return_type {
let tokens = self.type_abi_name(kind);
(
quote! { result_size__: *mut u32, result__: *mut *mut #tokens },
quote! { -> #hresult },
quote! {},
)
} else {
let tokens = self.type_abi_name(return_type);
(
quote! { result__: *mut #tokens },
quote! { -> #hresult },
quote! {},
)
}
} else {
(quote! {}, quote! { -> #hresult }, quote! {})

let crate_name = self.crate_name();

let (trailing_return_type, return_type, udt_return_type) = match &signature.return_type {
Type::Void if is_winrt => (quote! {}, quote! { -> #crate_name HRESULT }, quote! {}),
Type::Void => (quote! {}, quote! {}, quote! {}),
Type::WinrtArray(kind) => {
let tokens = self.type_abi_name(kind);
(
quote! { result_size__: *mut u32, result__: *mut *mut #tokens },
quote! { -> #crate_name HRESULT },
quote! {},
)
}
_ if is_winrt => {
let tokens = self.type_abi_name(&signature.return_type);
(
quote! { result__: *mut #tokens },
quote! { -> #crate_name HRESULT },
quote! {},
)
}
} else if let Some(return_type) = &signature.return_type {
if self.reader.type_is_struct(return_type) {
let tokens = self.type_abi_name(return_type);
_ if self.reader.type_is_struct(&signature.return_type) => {
let tokens = self.type_abi_name(&signature.return_type);
(quote! {}, quote! {}, quote! { result__: *mut #tokens, })
} else {
let tokens = self.type_default_name(return_type);
}
_ => {
let tokens = self.type_default_name(&signature.return_type);
(quote! {}, quote! { -> #tokens }, quote! {})
}
} else {
(quote! {}, quote! {}, quote! {})
};

let params = signature.params.iter().map(|p| {
Expand All @@ -967,7 +965,7 @@ impl<'a> Gen<'a> {
{
if p.ty.is_winrt_array() {
quote! { #abi_size_name: u32, #name: *const #abi, }
} else if p.ty.is_winrt_const_ref() {
} else if p.ty.is_const_ref() {
quote! { #name: &#abi, }
} else {
quote! { #name: #abi, }
Expand Down Expand Up @@ -1001,13 +999,13 @@ impl<'a> Gen<'a> {
to_ident(&self.reader.param_name(param).to_lowercase())
}
pub fn return_sig(&self, signature: &Signature) -> TokenStream {
if let Some(return_type) = &signature.return_type {
let tokens = self.type_default_name(return_type);
format!(" -> {}", tokens.as_str()).into()
} else if self.reader.method_def_does_not_return(signature.def) {
" -> !".into()
} else {
" -> ()".into()
match &signature.return_type {
Type::Void if self.reader.method_def_does_not_return(signature.def) => " -> !".into(),
Type::Void => " -> ()".into(),
_ => {
let tokens = self.type_default_name(&signature.return_type);
format!(" -> {}", tokens.as_str()).into()
}
}
}
pub fn win32_args(&self, params: &[SignatureParam], kind: SignatureKind) -> TokenStream {
Expand Down Expand Up @@ -1207,16 +1205,17 @@ impl<'a> Gen<'a> {
.iter()
.map(|p| self.winrt_produce_type(p, !is_delegate));

let return_type = if let Some(return_type) = &signature.return_type {
let tokens = self.type_name(return_type);
let return_type = match &signature.return_type {
Type::Void => quote! { () },
_ => {
let tokens = self.type_name(&signature.return_type);

if return_type.is_winrt_array() {
quote! { ::windows_core::Array<#tokens> }
} else {
tokens
if signature.return_type.is_winrt_array() {
quote! { ::windows_core::Array<#tokens> }
} else {
tokens
}
}
} else {
quote! { () }
};

let this = if is_delegate {
Expand Down
19 changes: 12 additions & 7 deletions crates/libs/bindgen/src/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ fn standalone_imp(gen: &mut Gen, names: &[&str]) -> String {
let mut constants = BTreeSet::new();

for name in names {
let mut found = false;
let type_name = TypeName::parse(name);

for def in gen.reader.get(type_name) {
found = true;
gen.reader
.type_collect_standalone(&Type::TypeDef((def, vec![])), &mut types);
}
Expand All @@ -49,12 +51,11 @@ fn standalone_imp(gen: &mut Gen, names: &[&str]) -> String {
.namespace_functions(type_name.namespace)
.filter(|method| gen.reader.method_def_name(*method) == type_name.name)
{
found = true;
functions.insert(method);
let signature = gen.reader.method_def_signature(method, &[]);
signature
.return_type
.iter()
.for_each(|ty| gen.reader.type_collect_standalone(ty, &mut types));
gen.reader
.type_collect_standalone(&signature.return_type, &mut types);
signature
.params
.iter()
Expand All @@ -66,6 +67,7 @@ fn standalone_imp(gen: &mut Gen, names: &[&str]) -> String {
.namespace_constants(type_name.namespace)
.find(|field| gen.reader.field_name(*field) == type_name.name)
{
found = true;
constants.insert(field);
gen.reader.type_collect_standalone(
&gen.reader.field_type(field, None).to_const_type(),
Expand All @@ -86,10 +88,13 @@ fn standalone_imp(gen: &mut Gen, names: &[&str]) -> String {
None
})
{
found = true;
constants.insert(field);
gen.reader
.type_collect_standalone(&gen.reader.field_type(field, None), &mut types);
}

assert!(found, "{} not found", type_name);
}

let mut sorted = SortedTokens::default();
Expand Down Expand Up @@ -173,9 +178,9 @@ fn standalone_imp(gen: &mut Gen, names: &[&str]) -> String {
sorted.insert(gen.reader.type_def_name(def), enums::gen(gen, def));
}
TypeKind::Struct => {
let name = gen.reader.type_def_name(def);
if gen.reader.type_def_fields(def).next().is_none() {
if let Some(guid) = gen.reader.type_def_guid(def) {
let name = gen.reader.type_def_name(def);
let ident = to_ident(name);
let value = gen.guid(&guid);
let guid = gen.type_name(&Type::GUID);
Expand All @@ -188,7 +193,7 @@ fn standalone_imp(gen: &mut Gen, names: &[&str]) -> String {
continue;
}
}
sorted.insert(gen.reader.type_def_name(def), structs::gen(gen, def));
sorted.insert(name, structs::gen(gen, def));
}
TypeKind::Delegate => {
sorted.insert(gen.reader.type_def_name(def), delegates::gen(gen, def));
Expand All @@ -212,7 +217,7 @@ fn standalone_imp(gen: &mut Gen, names: &[&str]) -> String {

for constant in constants {
sorted.insert(
&format!("{}", gen.reader.field_name(constant)),
gen.reader.field_name(constant),
constants::gen(gen, constant),
);
}
Expand Down
Loading