From 3fe9084e47e38245c4a76bda8e073c50753755bd Mon Sep 17 00:00:00 2001 From: yamachu Date: Wed, 6 Sep 2023 22:27:06 +0900 Subject: [PATCH] Add reference type parser --- csbindgen/src/parser.rs | 62 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/csbindgen/src/parser.rs b/csbindgen/src/parser.rs index bdfba17..5c918d1 100644 --- a/csbindgen/src/parser.rs +++ b/csbindgen/src/parser.rs @@ -476,6 +476,68 @@ fn parse_type(t: &syn::Type) -> RustType { type_kind: TypeKind::Function(parameters, ret), }; } + syn::Type::Reference(t) => { + let result = parse_type(&*t.elem); + let is_mut = t.mutability.is_some(); + + match result { + RustType { + type_kind: TypeKind::Pointer(pt, _), + .. + } => match pt { + PointerType::ConstPointer | PointerType::MutPointer => { + return RustType { + type_name: result.type_name, + type_kind: TypeKind::Pointer( + if is_mut { + PointerType::MutPointer + } else { + PointerType::ConstPointer + }, + Box::new(parse_type(&*t.elem)), + ), + }; + } + PointerType::ConstPointerPointer | PointerType::MutConstPointerPointer => { + return RustType { + type_name: result.type_name, + type_kind: TypeKind::Pointer( + if is_mut { + PointerType::MutConstPointerPointer + } else { + PointerType::ConstPointerPointer + }, + Box::new(parse_type(&*t.elem)), + ), + }; + } + PointerType::ConstMutPointerPointer | PointerType::MutPointerPointer => { + return RustType { + type_name: result.type_name, + type_kind: TypeKind::Pointer( + if is_mut { + PointerType::MutPointerPointer + } else { + PointerType::ConstMutPointerPointer + }, + Box::new(parse_type(&*t.elem)), + ), + }; + } + _ => {} + }, + _ => { + // &*t.elem is not pointer + return RustType { + type_name: result.type_name, + type_kind: TypeKind::Pointer( + PointerType::ConstPointer, + Box::new(parse_type(&*t.elem)), + ), + }; + } + } + } _ => {} };