directcpp-macro 0.1.12

Direct call cpp from Rust (proc-macro)
Documentation
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
mod code_match;
mod mangle;
mod parse;
mod util;
mod tests;

use parse::{Functions, map_to_cxx};
use std::collections::{HashMap, HashSet};
use proc_macro2::{TokenStream, Span};
use proc_macro::{TokenStream as TS0, TokenTree};
use std::env;
use regex::Regex;
use syn;
use std::str::FromStr;
use std::sync::Mutex;
use crate::mangle::*;
use crate::util::*;


const TYPE_POD:i32 = 0;
const TYPE_DTOR_TRIVIAL_MOVE:i32 = 1;  // 假定所有类型默认都是trivial move, non-trivial dtor

lazy_static::lazy_static! {
	static ref TYPE_STRATEGY: Mutex<HashMap<String, i32>> = Mutex::new(HashMap::new());
}

#[derive(Default)]
#[allow(dead_code)]
pub(crate) struct FFIBuilder{
	is_cpp: bool,
	extc_code: String,
	norm_code: String,
	err_str: String,
	asm_used: bool
}

impl FFIBuilder {
	pub fn new(reset:bool) -> Self {
		if reset {
			TYPE_STRATEGY.lock().unwrap().clear();
		}
		Self::default()
	}

	fn dtor_code(tp: &str) -> String {
		let tp = map_to_cxx(tp);
		let dtor_name = dtor_name(tp);
		let tp1 = tp.replace("<", "_").replace(">", "_");
		format!("\t#[link_name = \"{dtor_name}\"]\n\tfn ffi__free_{tp1}(__o: *mut usize);\n")
	}
	fn sp_dtor_code(tp: &str) -> String {
		let dtor_name = sp_dtor_name(tp);
		format!("\t#[link_name = \"{dtor_name}\"]\n\tfn ffi__freeSP_{tp}(__o: *mut usize);\n")
	}

	fn show_dtor(self: &mut Self, tp: &str, rtwrap:&str, tp_cpp: &str)->Result<(), &str> {
		let tp_strategy = match rtwrap {
			"POD" => TYPE_POD,
			// type can be dtor, and can be trivial move. trivial move is required for Rust.
			"SharedPtr"|"UniquePtr"|"Vec"|"" => TYPE_DTOR_TRIVIAL_MOVE,
			_ => {
				self.err_str = format!("type {} not supported", rtwrap);
				return Err(&self.err_str);
			}
		};
		let mut mp = TYPE_STRATEGY.lock().unwrap();
		let mut tp1 = if let Some(x) = mp.get(tp_cpp) {
			if *x >> 16 != tp_strategy {
				self.err_str = format!("type {tp} strategy conflict");
				return Err(&self.err_str);
			}
			*x
		} else {
			tp_strategy << 16
		};

		if tp_strategy != TYPE_POD {
			if (tp1 & 1) == 0 && rtwrap != "SharedPtr" {
				tp1 |= 1;
				self.extc_code += &Self::dtor_code(tp_cpp);
			}
			if rtwrap == "UniquePtr" && tp1 & 2 == 0 {
				tp1 |= 2;
				self.norm_code += &format!("
impl ManDtor for {tp} {{
	unsafe fn __dtor(ptr: *mut [u8;0]) {{
		if ptr as usize != 0 {{
			ffi__free_{tp}(ptr as *mut usize);
		}}
	}}
}}");
			}
			if rtwrap == "SharedPtr" && tp1 & 4 == 0 {
				tp1 |= 4;
				self.extc_code += &Self::sp_dtor_code(tp);
				self.norm_code += &format!("
impl DropSP for {tp} {{
	unsafe fn __drop_sp(ptr: *mut [u8;0]) {{
		if ptr as usize != 0 {{
			ffi__freeSP_{tp}(ptr as *mut usize);
		}}
	}}
}}\n");
			}
		}
		mp.insert(tp_cpp.to_string(), tp1);
		Ok(())
	}

	fn get_link_name(self: &Self, func: &SimpFunc, is_cpp: bool)
		-> Result<String, &'static str>
	{
		if ! is_cpp {
			Ok(func.fn_name.to_string())
		} else {
			mangle(&func)
		}
	}

	fn build_one_func(self:&mut Self, func: &SimpFunc, is_cpp: bool) -> Result<(), &str>{
		let mut args_c = Vec::new();
		let mut args_r = Vec::new();
		let mut args_usage = Vec::new();
		let mut fn_name =  if let Some(pos) = func.fn_name.rfind("::") {
			func.fn_name[pos + 2..].to_string()
		}else{
			func.fn_name.to_string()
		};
		if !func.klsname.is_empty() {
			args_c.push("this__: *const u8".to_string());
			args_r.push(format!("this__: CPtr<{}>", &func.klsname));
			args_usage.push("this__.addr as *const u8".to_string());
			fn_name = format!("{}__{}", &func.klsname, &func.fn_name);
		}
		let return_code_r = if func.is_async {
			if func.ret.tp.is_empty() {
				return Err("async function must have a return type");
			}
			if ! func.ret.tp_wrap.is_empty() {
				self.err_str = format!("currently async function should only return non-generic types. wrap your type into a struct if need. unsupported return type {}", &func.ret.raw_str);
				return Err(&self.err_str);
			}
			args_c.push("addr: usize".to_string());
			args_usage.push("dyn_fv_addr".to_string());
			format!(" -> {}", func.ret.tp_full)
		} else {
			match &func.ret.tp_wrap as &str {
				"" if func.ret.tp.is_empty() => String::new(),
				"POD" => format!(" -> {}", func.ret.tp),
				_ => format!(" -> {}", func.ret.tp_full),
			}
		};

		enum RetKind {
			RtPrimitive,
			RtCPtr,
			RtSharedPtr,
			RtObject,
		}
		let is_a64 = cfg!(target_arch="aarch64");
		let mut ret_indirect = String::new();
		let mut ret_kind = RetKind::RtPrimitive;
		let return_code_c = match &func.ret.tp_wrap as &str {
			"CPtr" => {
				ret_kind = RetKind::RtCPtr;
				" -> *const u8".to_string()
			},
			"SharedPtr"|"UniquePtr" => {
				ret_kind = RetKind::RtSharedPtr;
				if is_a64 {
					self.asm_used = true;
					ret_indirect = format!("let __rtox8 = &mut __rto as *mut {} as *mut u8;\n\t\t", &func.ret.tp_full);
				} else {
					args_c.push("__rto: * mut u8".to_string());
					args_usage.push(format!("&mut __rto as *mut {} as *mut u8", &func.ret.tp_full));
				}
				"".to_string()
			},
			"" if func.is_async => String::new(),
			"" if func.ret.tp.is_empty() => String::new(),
			"" if func.ret.is_primitive => format!(" -> {}", func.ret.tp),
			""|"POD"|"Vec" => {
				ret_kind = RetKind::RtObject;
				if is_a64 {
					self.asm_used = true;
					ret_indirect = "let __rtox8 = &mut __rta as *mut usize;\n\t\t".to_string();
				} else {
					args_c.push("__rto: * mut usize".to_string());
					args_usage.push("&mut __rta as *mut usize".to_string());
				}
				"".to_string()
			}
			_ => {
				self.err_str = format!("return type {} not supported", &func.ret.raw_str);
				return Err(&self.err_str);
			}
		};

		for arg in &func.arg_list {
			let mut args_x_done = false;
			let is_ref = arg.tp_full.chars().next().unwrap() == '&';
			match arg.tp_wrap.as_str() {
				""|"POD" if is_ref => {
					match arg.tp.as_str() {
						"CStr" => {
							args_x_done = true;
							args_c.push(format!("{}: *const i8", &arg.name));
							args_r.push(format!("{}: &CStr", &arg.name));
							args_usage.push(format!("{}.as_ptr()", &arg.name))
						},
						"str" => {
							args_x_done = true;
							args_c.push(format!("{}: *const u8, {}_len: usize", &arg.name, &arg.name));
							args_r.push(format!("{}: &str", &arg.name));
							args_usage.push(format!("{}.as_ptr()", &arg.name));
							args_usage.push(format!("{}.len()", &arg.name));
						},
						"[u8]" => {
							args_x_done = true;
							args_c.push(format!("{}: *const u8, {}_len: usize", &arg.name, &arg.name));
							args_r.push(format!("{}: &[u8]", &arg.name));
							args_usage.push(format!("{}.as_ptr()", &arg.name));
							args_usage.push(format!("{}.len()", &arg.name));
						},
						_ => args_usage.push(format!("{} as *{} {}", &arg.name, select_val(arg.is_const, "const", "mut"), &arg.tp)),
					}
				},
				"CPtr" => args_usage.push(format!("{}.addr as * const u8", &arg.name)),
				"Option" => match is_ref {
					true => args_usage.push(format!("{}.as_ref().map_or(0 as * const {}, |x| x as * const {})", &arg.name, &arg.tp, &arg.tp)),
					false => args_usage.push(format!("{}.map_or(0 as * const {}, |x| x as * const {})", &arg.name, &arg.tp, &arg.tp)),
				},
				// this is not tested, normally you should use CPtr<xx> for arguments, don't use these.
				"SharedPtr"|"UniquePtr" => args_usage.push(format!("{}.as_cptr().addr as * const {}", &arg.name, &arg.tp)),
				_ if arg.is_primitive => args_usage.push(format!("{}", &arg.name)),
				_ => {
					let suggested_str = arg.raw_str.replace(":", ": &");
					self.err_str = format!("function \"{}\" argument \"{}\" not supported, \
					you should always use a reference for non-primitive types in interop functions.\n\
					try use \"{}\" instead.", func.fn_name, &arg.raw_str, &suggested_str);
					return Err(&self.err_str);
				}
			};
			if !args_x_done {
				args_c.push(format!("{}: {}", &arg.name, &arg.tp_asc));
				args_r.push(format!("{}: {}", &arg.name, &arg.tp_full));
			}
		}

		let link_name = if func.is_async {
			let sa = SimpArg{
				name: "dyn_fv_addr".to_string(),
				tp: "usize".to_string(),
				tp_full: "usize".to_string(),
				tp_wrap: "".to_string(),
				tp_cpp: format!("ValuePromise<{}>*", func.ret.tp_cpp),
				is_const: false,
				is_primitive: true,
				raw_str: "usize".to_string(),
				tp_asc: "usize".to_string()
			};
			let mut func1 = func.clone();
			func1.ret.tp = "".to_string();
			func1.ret.tp_cpp = "".to_string();
			func1.ret.is_primitive = true;
			func1.arg_list.insert(0, sa);
			self.get_link_name(&func1, is_cpp)?
		} else {
			self.get_link_name(&func, is_cpp)?
		};
		let fnstart = format!("{} {}fn {}({}){}", &func.access,
			if func.is_async { "async " } else { "" },
			&fn_name, args_r.join(", "), return_code_r);
		self.extc_code += &format!("\t#[link_name = \"{link_name}\"]\n\tfn ffi__{fn_name}({}){};\n",
			args_c.join(", "), return_code_c);
		match ret_kind {
			RetKind::RtPrimitive => {},
			_ => {
				if let Err(s) = self.show_dtor(&func.ret.tp, &func.ret.tp_wrap, &func.ret.tp_cpp) {
					self.err_str = s.to_string();
					return Err(&self.err_str);
				}
			}
		}
		if ! ret_indirect.is_empty() {
			// \ asm!(\"mov x8, {{xval1}}\", xval1 = in(reg) __rtox8);
			//asm!(\"mov x8, {{x}}\", x = in(reg) __rtox8);
			if args_usage.len() > 0 {
				let idx = args_usage.len() - 1;
				let s0 = args_usage[idx].as_str();
				let s0 = format!("{{let __argk={}; asm!(\"mov x8, {{xval1}}\", xval1=in(reg) __rtox8); __argk}}", s0);
				args_usage[idx] = s0;
			} else {
				let s0 = "asm!(\"mov x8, {xval1}\", xval1=in(reg) __rtox8);\n\t\t";
				ret_indirect += s0;
			}
		}
		let usage = args_usage.join(", ");
		let norm_code = match ret_kind {
			RetKind::RtPrimitive if func.is_async => {
				format!("let mut fv= FutureValue::<{}>::default();\n\
					unsafe {{ let dyn_fv_addr = fv.to_ptr(); ffi__{fn_name}({usage}); }}\n\
					fv.await", &func.ret.tp)
			},
			RetKind::RtPrimitive => format!("unsafe {{ ffi__{fn_name}({usage}) }}"),
			RetKind::RtCPtr => format!("CPtr{{ addr: unsafe {{ ffi__{fn_name}({usage}) as usize }}, _phantom: std::marker::PhantomData }}"),
			RetKind::RtSharedPtr => {
				let wrap1 = &func.ret.tp_wrap as &str;
				let ret_type = &func.ret.tp as &str;
				format!("let mut __rto = {wrap1}::<{ret_type}>::default();\n\
					\tunsafe {{ {ret_indirect} ffi__{fn_name}({usage}); }}\n\
					\t__rto")
			},
			RetKind::RtObject => {
				let mut ret_type = &func.ret.tp_full as &str;
				let tp1 = func.ret.tp_cpp.replace("<", "_").replace(">", "_");
				let call_free = match func.ret.tp_wrap.as_str() {
					"POD" => {
						ret_type = &func.ret.tp as &str;
						"".to_string()
					},  // no destructor for POD
					_ => format!("ffi__free_{}(&mut __rta as *mut usize);\n\t\t", &tp1),
				};
				format!("const SZ:usize = (std::mem::size_of::<{ret_type}>()+16)/8;\n\
					\tlet mut __rta : [usize;SZ] = [0;SZ];\n\
					\tunsafe {{ {ret_indirect}\n\
					\t\tffi__{fn_name}({usage}); \n\
					\t\tlet __rto = (*(&__rta as *const usize as *const {ret_type})).clone();\n\
					\t\t{call_free}__rto\n\
					\t}}")
			},
			// _ => return Err("xx")
		};
		self.norm_code += &format!("{fnstart} {{\n\t{norm_code}\n}}\n");
		Ok(())
	}

	pub fn build_bridge_code(self: &mut Self, input: TokenStream) -> Result<TokenStream, &str> {
		let mut xxx = Functions::new();
		if let Err(s) = xxx.parse_ts(input) {
			self.err_str = s.to_string();
			return Err(&self.err_str);
		}

		for func in &xxx.funcs {
			if let Err(_) = self.build_one_func(func, xxx.is_cpp) {
				return Err(&self.err_str);
			}
		}
		let extc_code = move_obj(&mut self.extc_code);
		let norm_code = move_obj(&mut self.norm_code);
		let use_asm = select_val(self.asm_used, "use std::arch::asm;\n", "");
		let all_code = format!("{use_asm}extern \"C\" {{\n{extc_code}}}\n{norm_code}\n");
		if env_as_bool("RUST_BRIDGE_DEBUG") {
			println!("{}", all_code);
		}
		TokenStream::from_str(&all_code).map_err(|e| {
			self.err_str = e.to_string();
			self.err_str.as_str()
		})
	}
}

extern "C" {
	fn enable_msvc_debug_c();
}

#[proc_macro_attribute]
pub fn bridge(args: TS0, input: TS0) -> TS0 {
	let mut flags = HashSet::new();
	for tt in args.into_iter() {
		if let TokenTree::Ident(val) = tt {
			flags.insert(val.to_string());
		}
	}
	let mut bb = FFIBuilder::new(! flags.contains("goon") );
	match bb.build_bridge_code(input.into()) {
		Ok(code) => code.into(),
		Err(e) => syn::Error::new(Span::call_site(), e).to_compile_error().into()
	}
}

#[proc_macro_attribute]
pub fn enable_msvc_debug(args: TS0, _input: TS0) -> TS0
{
	let enable_ = if let Some(TokenTree::Ident(val)) = args.into_iter().next() {
		val.to_string().parse::<i32>().unwrap_or(-1)
	} else {
		-1
	};
	let is_debug = match enable_ {
		0 => false,
		1 => true,
		_ => {
			match env::var("OUT_DIR") {
				Ok(profile) => Regex::new(r"[\\/]target[\\/]debug[\\/]").unwrap().is_match(&profile),
				Err(_) => false,
			}
		}
	};
	if is_debug {
		unsafe{ enable_msvc_debug_c(); }
	}
	TS0::new()
}