| 1 |
use std::{ |
| 2 |
fmt::Write as FWrite, |
| 3 |
io::{Result, Write}, |
| 4 |
}; |
| 5 |
|
| 6 |
use log::error; |
| 7 |
|
| 8 |
use super::{ |
| 9 |
return_value::ToReturnValue, trampoline_from_glib::TrampolineFromGlib, |
| 10 |
trampoline_to_glib::TrampolineToGlib, |
| 11 |
}; |
| 12 |
use crate::{ |
| 13 |
analysis::{ |
| 14 |
bounds::Bounds, ffi_type::ffi_type, ref_mode::RefMode, rust_type::RustType, |
| 15 |
trampoline_parameters::*, trampolines::Trampoline, try_from_glib::TryFromGlib, |
| 16 |
}, |
| 17 |
consts::TYPE_PARAMETERS_START, |
| 18 |
env::Env, |
| 19 |
library, |
| 20 |
nameutil::{use_glib_if_needed, use_glib_type, use_gtk_type}, |
| 21 |
traits::IntoString, |
| 22 |
writer::primitives::tabs, |
| 23 |
}; |
| 24 |
|
| 25 |
pub fn generate( |
| 26 |
w: &mut dyn Write, |
| 27 |
env: &Env, |
| 28 |
analysis: &Trampoline, |
| 29 |
in_trait: bool, |
| 30 |
indent: usize, |
| 31 |
) -> Result<()> { |
| 32 |
let (self_bound, fn_self_bound) = if in_trait { |
| 33 |
{ |
| 34 |
( |
| 35 |
format!("{}: IsA<{}>, ", TYPE_PARAMETERS_START, analysis.type_name), |
| 36 |
Some(TYPE_PARAMETERS_START.to_string()), |
| 37 |
) |
| 38 |
} |
| 39 |
} else { |
| 40 |
Default::default() |
| 41 |
}; |
| 42 |
|
| 43 |
let prepend = tabs(indent); |
| 44 |
let params_str = trampoline_parameters(env, analysis); |
| 45 |
let func_str = func_string(env, analysis, fn_self_bound, true); |
| 46 |
let ret_str = trampoline_returns(env, analysis); |
| 47 |
|
| 48 |
writeln!( |
| 49 |
w, |
| 50 |
"{}unsafe extern \"C\" fn {}<{}F: {}>({}, f: {}){} {{ unsafe {{", |
| 51 |
prepend, |
| 52 |
analysis.name, |
| 53 |
self_bound, |
| 54 |
func_str, |
| 55 |
params_str, |
| 56 |
use_glib_if_needed(env, "ffi::gpointer"), |
| 57 |
ret_str, |
| 58 |
)?; |
| 59 |
writeln!(w, "{prepend}\tlet f: &F = &*(f as *const F);")?; |
| 60 |
transformation_vars(w, env, analysis, &prepend)?; |
| 61 |
let call = trampoline_call_func(env, analysis, in_trait); |
| 62 |
writeln!(w, "{prepend}\t{call}")?; |
| 63 |
writeln!(w, "{prepend}}}}}")?; |
| 64 |
|
| 65 |
Ok(()) |
| 66 |
} |
| 67 |
|
| 68 |
pub fn func_string( |
| 69 |
env: &Env, |
| 70 |
analysis: &Trampoline, |
| 71 |
replace_self_bound: Option<impl AsRef<str>>, |
| 72 |
closure: bool, |
| 73 |
) -> String { |
| 74 |
let param_str = func_parameters(env, analysis, replace_self_bound, closure); |
| 75 |
let return_str = func_returns(env, analysis); |
| 76 |
|
| 77 |
if closure { |
| 78 |
let concurrency_str = match analysis.concurrency { |
| 79 |
// If an object can be Send to other threads, this means that |
| 80 |
// our callback will be called from whatever thread the object |
| 81 |
// is sent to. But it will only be ever owned by a single thread |
| 82 |
// at a time, so signals can only be emitted from one thread at |
| 83 |
// a time and Sync is not needed |
| 84 |
library::Concurrency::Send => " + Send", |
| 85 |
// If an object is Sync, it can be shared between threads, and as |
| 86 |
// such our callback can be called from arbitrary threads and needs |
| 87 |
// to be Send *AND* Sync |
| 88 |
library::Concurrency::SendSync => " + Send + Sync", |
| 89 |
library::Concurrency::None => "", |
| 90 |
}; |
| 91 |
|
| 92 |
format!("Fn({param_str}){return_str}{concurrency_str} + 'static") |
| 93 |
} else { |
| 94 |
format!("({param_str}){return_str}",) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
fn func_parameters( |
| 99 |
env: &Env, |
| 100 |
analysis: &Trampoline, |
| 101 |
replace_self_bound: Option<impl AsRef<str>>, |
| 102 |
closure: bool, |
| 103 |
) -> String { |
| 104 |
let mut param_str = String::with_capacity(100); |
| 105 |
|
| 106 |
for (pos, par) in analysis.parameters.rust_parameters.iter().enumerate() { |
| 107 |
if pos == 0 { |
| 108 |
if let Some(replace_self_bound) = &replace_self_bound { |
| 109 |
param_str.push_str(par.ref_mode.for_rust_type()); |
| 110 |
param_str.push_str(replace_self_bound.as_ref()); |
| 111 |
continue; |
| 112 |
} |
| 113 |
} else { |
| 114 |
param_str.push_str(", "); |
| 115 |
if !closure { |
| 116 |
write!(param_str, "{}: ", par.name).unwrap(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
let s = func_parameter(env, par, &analysis.bounds); |
| 121 |
param_str.push_str(&s); |
| 122 |
} |
| 123 |
|
| 124 |
param_str |
| 125 |
} |
| 126 |
|
| 127 |
fn func_parameter(env: &Env, par: &RustParameter, bounds: &Bounds) -> String { |
| 128 |
// TODO: restore mutable support |
| 129 |
let ref_mode = if par.ref_mode == RefMode::ByRefMut { |
| 130 |
RefMode::ByRef |
| 131 |
} else { |
| 132 |
par.ref_mode |
| 133 |
}; |
| 134 |
|
| 135 |
match bounds.get_parameter_bound(&par.name) { |
| 136 |
// TODO: ASYNC?? |
| 137 |
Some(bound) => bound.full_type_parameter_reference(ref_mode, par.nullable, false), |
| 138 |
// TODO |
| 139 |
// Some((None, _)) => panic!("Trampoline expects type name"), |
| 140 |
None => RustType::builder(env, par.typ) |
| 141 |
.direction(par.direction) |
| 142 |
.nullable(par.nullable) |
| 143 |
.ref_mode(ref_mode) |
| 144 |
.try_build_param() |
| 145 |
.into_string(), |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
fn func_returns(env: &Env, analysis: &Trampoline) -> String { |
| 150 |
if analysis.ret.typ == Default::default() { |
| 151 |
String::new() |
| 152 |
} else if analysis.inhibit { |
| 153 |
format!(" -> {inhibit}", inhibit = use_glib_type(env, "Propagation")) |
| 154 |
} else if let Some(return_type) = |
| 155 |
analysis |
| 156 |
.ret |
| 157 |
.to_return_value(env, &TryFromGlib::default(), true) |
| 158 |
{ |
| 159 |
format!(" -> {return_type}") |
| 160 |
} else { |
| 161 |
String::new() |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
fn trampoline_parameters(env: &Env, analysis: &Trampoline) -> String { |
| 166 |
if analysis.is_notify { |
| 167 |
return format!( |
| 168 |
"{}, _param_spec: {}", |
| 169 |
trampoline_parameter(env, &analysis.parameters.c_parameters[0]), |
| 170 |
use_glib_if_needed(env, "ffi::gpointer"), |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
let mut parameter_strs: Vec<String> = Vec::new(); |
| 175 |
for par in &analysis.parameters.c_parameters { |
| 176 |
let par_str = trampoline_parameter(env, par); |
| 177 |
parameter_strs.push(par_str); |
| 178 |
} |
| 179 |
|
| 180 |
parameter_strs.join(", ") |
| 181 |
} |
| 182 |
|
| 183 |
fn trampoline_parameter(env: &Env, par: &CParameter) -> String { |
| 184 |
let ffi_type = ffi_type(env, par.typ, &par.c_type); |
| 185 |
format!("{}: {}", par.name, ffi_type.into_string()) |
| 186 |
} |
| 187 |
|
| 188 |
fn trampoline_returns(env: &Env, analysis: &Trampoline) -> String { |
| 189 |
if analysis.ret.typ == Default::default() { |
| 190 |
String::new() |
| 191 |
} else { |
| 192 |
let ffi_type = ffi_type(env, analysis.ret.typ, &analysis.ret.c_type); |
| 193 |
format!(" -> {}", ffi_type.into_string()) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
fn transformation_vars( |
| 198 |
w: &mut dyn Write, |
| 199 |
env: &Env, |
| 200 |
analysis: &Trampoline, |
| 201 |
prepend: &str, |
| 202 |
) -> Result<()> { |
| 203 |
use crate::analysis::trampoline_parameters::TransformationType::*; |
| 204 |
for transform in &analysis.parameters.transformations { |
| 205 |
match transform.transformation { |
| 206 |
None => (), |
| 207 |
Borrow => (), |
| 208 |
TreePath => { |
| 209 |
let c_par = &analysis.parameters.c_parameters[transform.ind_c]; |
| 210 |
writeln!( |
| 211 |
w, |
| 212 |
"{}\tlet {} = from_glib_full({}({}));", |
| 213 |
prepend, |
| 214 |
transform.name, |
| 215 |
use_gtk_type(env, "ffi::gtk_tree_path_new_from_string"), |
| 216 |
c_par.name |
| 217 |
)?; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
Ok(()) |
| 222 |
} |
| 223 |
|
| 224 |
fn trampoline_call_func(env: &Env, analysis: &Trampoline, in_trait: bool) -> String { |
| 225 |
let params = trampoline_call_parameters(env, analysis, in_trait); |
| 226 |
let ret = if analysis.ret.typ == Default::default() { |
| 227 |
String::new() |
| 228 |
} else { |
| 229 |
analysis.ret.trampoline_to_glib(env) |
| 230 |
}; |
| 231 |
format!("f({params}){ret}") |
| 232 |
} |
| 233 |
|
| 234 |
fn trampoline_call_parameters(env: &Env, analysis: &Trampoline, in_trait: bool) -> String { |
| 235 |
let mut need_downcast = in_trait; |
| 236 |
let mut parameter_strs: Vec<String> = Vec::new(); |
| 237 |
for (ind, par) in analysis.parameters.rust_parameters.iter().enumerate() { |
| 238 |
let transformation = match analysis.parameters.get(ind) { |
| 239 |
Some(transformation) => transformation, |
| 240 |
None => { |
| 241 |
error!("No transformation for {}", par.name); |
| 242 |
continue; |
| 243 |
} |
| 244 |
}; |
| 245 |
let par_str = transformation.trampoline_from_glib(env, need_downcast, *par.nullable); |
| 246 |
parameter_strs.push(par_str); |
| 247 |
need_downcast = false; // Only downcast first parameter |
| 248 |
} |
| 249 |
|
| 250 |
parameter_strs.join(", ") |
| 251 |
} |