1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
use std::borrow::Cow;
use std::fmt::Write;
use std::iter;

use maplit::hashmap;
use once_cell::sync::Lazy;
use regex::Regex;

use crate::func::{Kind, OperatorKind};
use crate::type_ref::{ConstnessOverride, CppNameStyle, Dir, ExternDir, FishStyle, NameStyle, StrEnc, StrType};
use crate::{
	get_debug, reserved_rename, settings, CompiledInterpolation, Element, EntityElement, Func, IteratorExt, StrExt, StringExt,
	TypeRef,
};

use super::comment;
use super::element::{DefaultRustNativeElement, RustElement};
use super::func_desc::{pre_post_arg_handle, CppFuncDesc, FuncDescCppCall, FuncDescKind};
use super::type_ref::TypeRefExt;
use super::{rust_disambiguate_names, RustNativeGeneratedElement};

pub fn disambiguate_single_name(name: &str) -> impl Iterator<Item = String> + '_ {
	let mut i = 0;
	iter::from_fn(move || {
		let out = format!("{}{}", name, disambiguate_num(i));
		i += 1;
		Some(out)
	})
}

pub fn disambiguate_num(counter: usize) -> String {
	match counter {
		0 => "".to_string(),
		n => format!("_{n}"),
	}
}

fn gen_rust_with_name(f: &Func, name: &str, opencv_version: &str) -> String {
	static TPL: Lazy<CompiledInterpolation> = Lazy::new(|| include_str!("tpl/func/rust.tpl.rs").compile_interpolation());

	let args = rust_disambiguate_names(f.arguments()).collect::<Vec<_>>();
	let kind = f.kind();
	let as_instance_method = kind.as_instance_method();
	let method_constness = f.constness();
	let is_infallible = f.is_infallible();
	let mut decl_args = Vec::with_capacity(args.len());
	let mut call_args = Vec::with_capacity(args.len());
	let mut forward_args = Vec::with_capacity(args.len());
	let mut pre_call_args = Vec::with_capacity(args.len());
	let mut post_call_args = Vec::with_capacity(args.len());
	if let Some(cls) = as_instance_method {
		decl_args.push(cls.type_ref().rust_self_func_decl(method_constness));
		call_args.push(cls.type_ref().rust_self_func_call(method_constness));
	}
	let mut callback_arg_name: Option<String> = None;
	for (name, arg) in args {
		let type_ref = arg.type_ref();
		if arg.is_user_data() {
			pre_post_arg_handle(
				type_ref.rust_userdata_pre_call(
					&name,
					callback_arg_name.as_deref().expect("Can't get name of the callback arg"),
				),
				&mut pre_call_args,
			);
		} else {
			if type_ref.as_function().is_some() {
				callback_arg_name = Some(name.clone());
			}
			if !arg.as_slice_len().is_some() {
				decl_args.push(type_ref.rust_arg_func_decl(&name));
			}
			pre_post_arg_handle(type_ref.rust_arg_pre_call(&name, is_infallible), &mut pre_call_args);
		}
		if let Some((slice_arg, len_div)) = arg.as_slice_len() {
			let slice_call = if len_div > 1 {
				format!(
					"({slice_arg}.len() / {len_div}) as _"
				)
			} else {
				format!("{slice_arg}.len() as _")
			};
			call_args.push(slice_call);
		} else {
			call_args.push(type_ref.rust_arg_func_call(&name, ConstnessOverride::No));
		}
		forward_args.push(type_ref.rust_arg_forward(&name));
		pre_post_arg_handle(type_ref.rust_arg_post_call(&name, is_infallible), &mut post_call_args);
	}
	let naked_return = f.is_naked_return();
	if !naked_return {
		pre_call_args.push("return_send!(via ocvrs_return);".to_string());
		call_args.push("ocvrs_return.as_mut_ptr()".to_string());
	}

	let doc_comment = f.rendered_doc_comment(opencv_version);
	let debug = get_debug(f);
	let visibility = if let Some(cls) = as_instance_method {
		if cls.is_trait() {
			""
		} else {
			"pub "
		}
	} else {
		"pub "
	};
	let identifier = f.identifier();
	let is_safe = !f.is_unsafe();
	let is_static_func = matches!(f.kind(), Kind::StaticMethod(..) | Kind::Function);
	let return_type = f.return_type();
	let return_type_func_decl = if is_infallible {
		return_type.rust_return(FishStyle::No, is_static_func)
	} else {
		format!("Result<{}>", return_type.rust_return(FishStyle::No, is_static_func)).into()
	};
	let return_type_func_decl = if return_type_func_decl == "()" {
		Cow::Borrowed("")
	} else {
		format!(" -> {return_type_func_decl}").into()
	};
	if is_infallible {
		post_call_args.push("ret".to_string());
	} else {
		post_call_args.push("Ok(ret)".to_string());
	}
	let ret_receive = if naked_return {
		"let ret = "
	} else {
		""
	};
	let mut ret_convert = Vec::with_capacity(3);
	if !naked_return {
		let spec = if is_safe {
			"return_receive!(unsafe ocvrs_return => ret);"
		} else {
			"return_receive!(ocvrs_return => ret);"
		};
		ret_convert.push(Cow::Borrowed(spec));
	}
	if !is_infallible {
		ret_convert.push("let ret = ret.into_result()?;".into())
	}
	let ret_map = rust_return_map(&return_type, "ret", is_safe, is_static_func, is_infallible);
	if !ret_map.is_empty() {
		ret_convert.push(format!("let ret = {ret_map};").into());
	}
	let mut attributes = String::new();
	if let Some((rust_attr, _)) = settings::FUNC_CFG_ATTR.get(identifier.as_ref()) {
		attributes = format!("#[cfg({rust_attr})]");
	}
	if f.is_no_discard() {
		attributes.push_str("#[must_use]");
	}

	let decl_args = decl_args.join(", ");
	let pre_call_args = pre_call_args.join("\n");
	let call_args = call_args.join(", ");
	let forward_args = forward_args.join(", ");
	let post_call_args = post_call_args.join("\n");
	let ret_convert = ret_convert.join("\n");
	let tpl = if let Some(tpl) = settings::FUNC_MANUAL.get(identifier.as_ref()) {
		tpl
	} else {
		&TPL
	};
	tpl.interpolate(&hashmap! {
		"doc_comment" => doc_comment.as_str(),
		"debug" => &debug,
		"attributes" => &attributes,
		"visibility" => visibility,
		"unsafety_decl" => if is_safe { "" } else { "unsafe " },
		"name" => name,
		"generic_decl" => "",
		"decl_args" => &decl_args,
		"rv_rust_full" => return_type_func_decl.as_ref(),
		"pre_call_args" => &pre_call_args,
		"unsafety_call" => if is_safe { "unsafe " } else { "" },
		"identifier" => identifier.as_ref(),
		"call_args" => &call_args,
		"forward_args" => &forward_args,
		"ret_receive" => ret_receive,
		"ret_convert" => ret_convert.as_ref(),
		"post_call_args" => &post_call_args,
	})
}

fn rust_return_map(
	return_type: &TypeRef,
	ret_name: &str,
	is_safe_context: bool,
	is_static_func: bool,
	is_infallible: bool,
) -> Cow<'static, str> {
	let unsafety_call = if is_safe_context {
		"unsafe "
	} else {
		""
	};
	if return_type.as_string().is_some() || return_type.is_extern_by_ptr() {
		format!(
			"{unsafety_call}{{ {typ}::opencv_from_extern({ret_name}) }}",
			unsafety_call = unsafety_call,
			typ = return_type.rust_return(FishStyle::Turbo, is_static_func),
			ret_name = ret_name,
		)
		.into()
	} else if return_type.as_pointer().map_or(false, |i| !i.is_void()) && !return_type.is_rust_by_ptr()
		|| return_type.as_fixed_array().is_some()
	{
		let ptr_call = if return_type.constness().is_const() {
			"as_ref"
		} else {
			"as_mut"
		};
		let error_handling = if is_infallible {
			".expect(\"Function returned null pointer\")"
		} else {
			".ok_or_else(|| Error::new(core::StsNullPtr, \"Function returned null pointer\"))?"
		};
		format!(
			"{unsafety_call}{{ {ret_name}.{ptr_call}() }}{error_handling}",
		)
		.into()
	} else {
		"".into()
	}
}

pub fn cpp_return_map<'f>(return_type: &TypeRef, name: &'f str, is_constructor: bool) -> (Cow<'f, str>, bool) {
	if return_type.is_void() {
		("".into(), false)
	} else if return_type.is_extern_by_ptr() && !is_constructor {
		let out = return_type.source().as_class().filter(|cls| cls.is_abstract()).map_or_else(
			|| {
				format!(
					"new {typ}({name})",
					typ = return_type.cpp_name(CppNameStyle::Reference),
					name = name
				)
				.into()
			},
			|_| name.into(),
		);
		(out, false)
	} else if let Some(Dir::In(string_type)) | Some(Dir::Out(string_type)) = return_type.as_string() {
		let str_mk = match string_type {
			StrType::StdString(StrEnc::Text) | StrType::CvString(StrEnc::Text) => {
				format!("ocvrs_create_string({name}.c_str())").into()
			}
			StrType::CharPtr => format!("ocvrs_create_string({name})").into(),
			StrType::StdString(StrEnc::Binary) => {
				format!("ocvrs_create_byte_string({name}.data(), {name}.size())").into()
			}
			StrType::CvString(StrEnc::Binary) => {
				format!("ocvrs_create_byte_string({name}.begin(), {name}.size())").into()
			}
		};
		(str_mk, false)
	} else {
		(name.into(), return_type.as_fixed_array().is_some())
	}
}

pub fn cpp_return_handle(
	ret: &str,
	ret_cast: Option<Cow<str>>,
	ocv_ret_name: &str,
	is_naked_return: bool,
	is_infallible: bool,
) -> Cow<'static, str> {
	if is_naked_return {
		if ret.is_empty() {
			"".into()
		} else {
			let cast = if let Some(ret_type) = ret_cast {
				format!("({typ})", typ = ret_type.as_ref())
			} else {
				"".to_string()
			};
			format!("return {cast}{ret};").into()
		}
	} else if is_infallible {
		if ret.is_empty() {
			"".into()
		} else {
			format!("*{ocv_ret_name} = {ret};").into()
		}
	} else if ret.is_empty() {
		format!("Ok({ocv_ret_name});").into()
	} else {
		let cast = if let Some(ret_type) = ret_cast {
			format!("<{typ}>", typ = ret_type.as_ref())
		} else {
			"".to_string()
		};
		format!("Ok{cast}({ret}, {ocv_ret_name});").into()
	}
}

impl RustElement for Func<'_, '_> {
	fn rust_module(&self) -> Cow<str> {
		DefaultRustNativeElement::rust_module(self)
	}

	fn rust_name(&self, style: NameStyle) -> Cow<str> {
		DefaultRustNativeElement::rust_name(self, style)
	}

	fn rust_leafname(&self, _fish_style: FishStyle) -> Cow<str> {
		let cpp_name = if let Some(name) = self.gen_env().get_rename_config(self.entity()).map(|c| &c.rename) {
			name.into()
		} else {
			self.cpp_name(CppNameStyle::Declaration)
		};
		let kind = self.kind();
		let rust_name = if let Some(cls) = kind.as_constructor() {
			let args = self.arguments();
			#[allow(clippy::never_loop)] // fixme use named block when MSRV is 1.65
			'ctor_name: loop {
				if args.is_empty() {
					break 'ctor_name "default";
				} else if args.len() == 1 {
					let arg_typeref = args[0].type_ref();
					let class_arg = arg_typeref.as_reference().and_then(|typ| {
						if let Some(ptr) = typ.as_smart_ptr() {
							ptr.pointee()
						} else {
							typ
						}
						.as_class()
					});
					if let Some(other) = class_arg {
						if *cls == other {
							break 'ctor_name if arg_typeref.constness().is_const() {
								"copy"
							} else {
								"copy_mut"
							};
						}
					}
				}
				break 'ctor_name "new";
			}
			.into()
		} else if let Some(..) = kind.as_conversion_method() {
			let mut name: String = self.return_type().rust_name(NameStyle::decl()).into_owned();
			name.cleanup_name();
			format!("to_{name}").into()
		} else if let Some((cls, kind)) = kind.as_operator() {
			if cpp_name.starts_with("operator") {
				let name = match kind {
					OperatorKind::Unsupported => cpp_name.as_ref(),
					OperatorKind::Index => {
						if self.constness().is_const() {
							"get"
						} else {
							"get_mut"
						}
					}
					OperatorKind::Add => "add",
					OperatorKind::Sub => "sub",
					OperatorKind::Mul => "mul",
					OperatorKind::Div => "div",
					OperatorKind::Apply => "apply",
					OperatorKind::Deref => {
						if self.constness().is_const() {
							"try_deref"
						} else {
							"try_deref_mut"
						}
					}
					OperatorKind::Equals => "equals",
					OperatorKind::NotEquals => "not_equals",
					OperatorKind::GreaterThan => "greater_than",
					OperatorKind::GreaterThanOrEqual => "greater_than_or_equal",
					OperatorKind::LessThan => "less_than",
					OperatorKind::LessThanOrEqual => "less_than_or_equal",
					OperatorKind::Incr => "incr",
					OperatorKind::Decr => "decr",
					OperatorKind::And => "and",
					OperatorKind::Or => "or",
					OperatorKind::Xor => "xor",
					OperatorKind::BitwiseNot => "negate",
				};
				if kind.add_args_to_name() {
					let args = self.arguments();
					let is_single_arg_same_as_class = if let (Some(cls), [single_arg]) = (cls, args.as_slice()) {
						single_arg
							.type_ref()
							.source()
							.as_class()
							.map_or(false, |single_class| &single_class == cls)
					} else {
						false
					};
					if args.is_empty() || is_single_arg_same_as_class {
						name.into()
					} else {
						let args = args.into_iter().map(|arg| arg.type_ref().rust_simple_name()).join("_");
						format!("{name}_{args}").into()
					}
				} else {
					name.into()
				}
			} else {
				cpp_name
			}
		} else {
			cpp_name
		};
		if let Some(&name) = settings::FUNC_RENAME.get(self.identifier().as_ref()) {
			if name.contains('+') {
				reserved_rename(name.replace('+', rust_name.as_ref()).to_snake_case().into())
			} else {
				name.into()
			}
		} else {
			reserved_rename(rust_name.to_snake_case().into())
		}
	}

	fn rendered_doc_comment_with_prefix(&self, prefix: &str, opencv_version: &str) -> String {
		let mut comment = self.entity().get_comment().unwrap_or_default();
		let line = self.entity().get_location().map(|l| l.get_file_location().line).unwrap_or(0);
		const OVERLOAD: &str = "@overload";
		if let Some(idx) = comment.find(OVERLOAD) {
			let rep = if let Some(copy) = self
				.gen_env()
				.get_func_comment(line, self.entity().cpp_name(CppNameStyle::Reference).as_ref())
			{
				format!("{copy}\n\n## Overloaded parameters\n")
			} else {
				"This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.".to_string()
			};
			comment.replace_range(idx..idx + OVERLOAD.len(), &rep);
		}
		static COPY_BRIEF: Lazy<Regex> = Lazy::new(|| Regex::new(r#"@copybrief\s+(\w+)"#).unwrap());
		comment.replace_in_place_regex_cb(&COPY_BRIEF, |comment, caps| {
			let copy_name = caps.get(1).map(|(s, e)| &comment[s..e]).expect("Impossible");
			let mut copy_full_name = self.cpp_namespace().into_owned();
			copy_full_name += "::";
			copy_full_name += copy_name;
			if let Some(copy) = self.gen_env().get_func_comment(line, &copy_full_name) {
				Some(copy.into())
			} else {
				Some("".into())
			}
		});
		comment::render_doc_comment_with_processor(&comment, prefix, opencv_version, |out| {
			let mut default_args_comment = String::with_capacity(1024);
			for arg in self.arguments() {
				if let Some(def_val) = arg.default_value() {
					if default_args_comment.is_empty() {
						default_args_comment += "## C++ default parameters";
					}
					write!(
						&mut default_args_comment,
						"\n* {name}: {val}",
						name = arg.rust_leafname(FishStyle::No),
						val = def_val
					)
					.expect("write! to String shouldn't fail");
				}
			}
			if !default_args_comment.is_empty() {
				if !out.is_empty() {
					out.push_str("\n\n");
				}
				out.push_str(&default_args_comment);
			}
		})
	}
}

impl RustNativeGeneratedElement for Func<'_, '_> {
	fn element_safe_id(&self) -> String {
		format!("{}-{}", self.rust_module(), self.rust_name(NameStyle::decl()))
	}

	fn gen_rust(&self, opencv_version: &str) -> String {
		let name = if self.is_clone() {
			"try_clone".into()
		} else if let Some(name_hint) = self.name_hint() {
			name_hint.into()
		} else {
			self.rust_leafname(FishStyle::No)
		};
		gen_rust_with_name(self, name.as_ref(), opencv_version)
	}

	fn gen_rust_exports(&self) -> String {
		static TPL: Lazy<CompiledInterpolation> = Lazy::new(|| include_str!("tpl/func/rust_extern.tpl.rs").compile_interpolation());

		let identifier = self.identifier();

		if settings::FUNC_MANUAL.contains_key(identifier.as_ref()) {
			return "".to_string();
		}

		let mut attributes = String::new();
		if let Some((rust_attr, _)) = settings::FUNC_CFG_ATTR.get(identifier.as_ref()) {
			attributes = format!("#[cfg({rust_attr})]");
		}
		let mut args = vec![];
		if let Some(cls) = self.kind().as_instance_method() {
			args.push(cls.type_ref().rust_extern_self_func_decl(self.constness()));
		}
		for (name, arg) in rust_disambiguate_names(self.arguments()) {
			args.push(arg.type_ref().rust_extern_arg_func_decl(&name, ConstnessOverride::No))
		}

		let naked_return = self.is_naked_return();
		let is_infallible = self.is_infallible();
		let return_type = self.return_type();
		let return_wrapper_type = if is_infallible {
			return_type.rust_extern(ExternDir::FromCpp)
		} else {
			return_type.rust_extern_return_fallible()
		};
		if !naked_return {
			let ret_name = "ocvrs_return";
			args.push(format!("{ret_name}: *mut {return_wrapper_type}"));
		}
		let return_wrapper_type = if return_type.is_void() || !naked_return {
			"".into()
		} else {
			format!(" -> {return_wrapper_type}").into()
		};
		TPL.interpolate(&hashmap! {
			"attributes" => attributes.into(),
			"debug" => get_debug(self).into(),
			"identifier" => identifier,
			"args" => args.join(", ").into(),
			"return_type" => return_wrapper_type,
		})
	}

	fn gen_cpp(&self) -> String {
		CppFuncDesc::from(self).gen_cpp()
	}
}

impl<'tu, 'ge, 'r> From<&'r Func<'tu, 'ge>> for CppFuncDesc<'tu, 'ge, 'r> {
	fn from(f: &'r Func<'tu, 'ge>) -> Self {
		let extern_name = f.identifier();
		let constness = f.constness();
		let is_infallible = f.is_infallible();
		let is_naked_return = f.is_naked_return();
		let return_type = f.return_type();
		let kind = FuncDescKind::from(f.kind());
		let type_hint = f.type_hint();
		let name_decl = f.cpp_name(CppNameStyle::Declaration);
		let name_ref = f.cpp_name(CppNameStyle::Reference);
		let debug = get_debug(f);
		let arguments = f
			.arguments()
			.into_iter()
			.map(|arg| (arg.cpp_name(CppNameStyle::Declaration).into_owned(), arg.type_ref()))
			.collect();

		Self {
			extern_name,
			constness,
			is_infallible,
			is_naked_return,
			return_type,
			kind,
			type_hint,
			call: FuncDescCppCall::Auto { name_decl, name_ref },
			debug,
			arguments,
		}
	}
}