Skip to content

Commit 2262a1b

Browse files
authored
Preliminary support for variadic functions (#2457)
1 parent 21e5849 commit 2262a1b

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

crates/libs/bindgen/src/functions.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,22 +207,30 @@ fn gen_link(gen: &Gen, signature: &Signature, cfg: &Cfg) -> TokenStream {
207207

208208
let return_type = gen.return_sig(signature);
209209

210+
// TODO: blocked on https://github.com/rust-lang/rust/issues/110505
211+
// let vararg = if signature.vararg {
212+
// "...".into()
213+
// } else {
214+
// quote! {}
215+
// };
216+
let vararg = quote! {};
217+
210218
if gen.std || !gen.namespace.starts_with("Windows.") {
211219
let library = library.trim_end_matches(".dll");
212220

213221
quote! {
214222
#[link(name = #library)]
215223
extern #abi {
216224
#link_name
217-
pub fn #ident(#(#params),*) #return_type;
225+
pub fn #ident(#(#params),* #vararg) #return_type;
218226
}
219227
}
220228
} else if let Some(library) = gen.reader.method_def_static_lib(signature.def) {
221229
quote! {
222230
#[link(name = #library, kind = "static")]
223231
extern #abi {
224232
#link_name
225-
pub fn #ident(#(#params),*) #return_type;
233+
pub fn #ident(#(#params),* #vararg) #return_type;
226234
}
227235
}
228236
} else {
@@ -242,6 +250,7 @@ fn gen_link(gen: &Gen, signature: &Signature, cfg: &Cfg) -> TokenStream {
242250
for param in params {
243251
tokens.push_str(&format!("{}, ", param.as_str()));
244252
}
253+
tokens.push_str(&vararg.0);
245254
let tokens = tokens.trim_end_matches(", ");
246255
format!(
247256
r#"::windows_targets::link!("{library}" "{abi}"{symbol}{doc} fn {name}({tokens}){return_type});"#

crates/libs/metadata/src/reader/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ pub struct Signature {
146146
pub def: MethodDef,
147147
pub params: Vec<SignatureParam>,
148148
pub return_type: Option<Type>,
149+
pub vararg: bool,
149150
}
150151

151152
pub struct SignatureParam {
@@ -603,9 +604,8 @@ impl<'a> Reader<'a> {
603604
}
604605
pub fn method_def_signature(&self, row: MethodDef, generics: &[Type]) -> Signature {
605606
let mut blob = self.row_blob(row.0, 4);
606-
blob.read_usize();
607-
blob.read_usize();
608-
607+
let vararg = blob.read_usize() == 0x05;
608+
let _param_count = blob.read_usize();
609609
let mut return_type = self.type_from_blob(&mut blob, None, generics);
610610

611611
let mut params: Vec<SignatureParam> = self
@@ -704,7 +704,7 @@ impl<'a> Reader<'a> {
704704
}
705705
}
706706

707-
Signature { def: row, params, return_type }
707+
Signature { def: row, params, return_type, vararg }
708708
}
709709
pub fn method_def_extern_abi(&self, def: MethodDef) -> &'static str {
710710
let impl_map = self.method_def_impl_map(def).expect("ImplMap not found");

crates/libs/targets/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ Learn more about Rust for Windows here: <https://github.com/microsoft/windows-rs
88
#[macro_export]
99
#[doc(hidden)]
1010
macro_rules! link {
11-
($library:literal $abi:literal $($link_name:literal)? $(#[$($doc:tt)*])* fn $name:ident($($arg:ident: $argty:ty),*)$(->$ret:ty)?) => (
11+
($library:literal $abi:literal $($link_name:literal)? $(#[$doc:meta])? fn $($function:tt)*) => (
1212
#[link(name = $library, kind = "raw-dylib", modifiers = "+verbatim", import_name_type = "undecorated")]
1313
extern $abi {
14-
$(#[$($doc)*])*
14+
$(#[$doc])?
1515
$(#[link_name=$link_name])?
16-
pub fn $name($($arg: $argty),*) $(->$ret)?;
16+
pub fn $($function)*;
1717
}
1818
)
1919
}
@@ -22,12 +22,12 @@ macro_rules! link {
2222
#[macro_export]
2323
#[doc(hidden)]
2424
macro_rules! link {
25-
($library:literal $abi:literal $($link_name:literal)? $(#[$($doc:tt)*])* fn $name:ident($($arg:ident: $argty:ty),*)$(->$ret:ty)?) => (
25+
($library:literal $abi:literal $($link_name:literal)? $(#[$doc:meta])? fn $($function:tt)*) => (
2626
#[link(name = $library, kind = "raw-dylib", modifiers = "+verbatim")]
2727
extern "system" {
28-
$(#[$($doc)*])*
28+
$(#[$doc])?
2929
$(#[link_name=$link_name])?
30-
pub fn $name($($arg: $argty),*) $(->$ret)?;
30+
pub fn $($function)*;
3131
}
3232
)
3333
}
@@ -36,12 +36,12 @@ macro_rules! link {
3636
#[macro_export]
3737
#[doc(hidden)]
3838
macro_rules! link {
39-
($library:literal $abi:literal $($link_name:literal)? $(#[$($doc:tt)*])* fn $name:ident($($arg:ident: $argty:ty),*)$(->$ret:ty)?) => (
39+
($library:literal $abi:literal $($link_name:literal)? $(#[$doc:meta])? fn $($function:tt)*) => (
4040
#[link(name = "windows.0.48.0")]
4141
extern $abi {
42-
$(#[$($doc)*])*
42+
$(#[$doc])?
4343
$(#[link_name=$link_name])?
44-
pub fn $name($($arg: $argty),*) $(->$ret)?;
44+
pub fn $($function)*;
4545
}
4646
)
4747
}
@@ -50,10 +50,10 @@ macro_rules! link {
5050
#[macro_export]
5151
#[doc(hidden)]
5252
macro_rules! link {
53-
($library:literal $abi:literal $($link_name:literal)? $(#[$($doc:tt)*])* fn $name:ident($($arg:ident: $argty:ty),*)$(->$ret:ty)?) => (
53+
($library:literal $abi:literal $($link_name:literal)? $(#[$doc:meta])? fn $($function:tt)*) => (
5454
extern $abi {
55-
$(#[$($doc)*])*
56-
pub fn $name($($arg: $argty),*) $(->$ret)?;
55+
$(#[$doc])?
56+
pub fn $($function)*;
5757
}
5858
)
5959
}

0 commit comments

Comments
 (0)