Skip to main content

qtbridge_gen/utils/
type_utils.rs

1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only
3
4use proc_macro2::TokenStream;
5use quote::quote;
6use syn::spanned::Spanned;
7
8use crate::utils::type_to_string::{path_to_string_fallback, type_to_string, type_to_string_fallback};
9
10#[derive(Copy, Clone, PartialEq)]
11pub enum ValuePassBy {
12    Value,
13    Ref,
14    MutRef,
15}
16
17pub fn remove_ref_to_string(ty: &syn::Type) -> syn::Result<String> {
18    type_to_string(remove_refs(ty))
19}
20
21/// Recursively unwraps the type until non-reference type is found.
22pub fn remove_refs(ty: &syn::Type) -> &syn::Type {
23    let mut unwrapped = ty;
24    while let syn::Type::Reference(type_ref) = unwrapped {
25        unwrapped = type_ref.elem.as_ref()
26    }
27    unwrapped
28}
29
30/// Remove one layer of reference if the type is a reference.
31pub fn remove_ref(ty: &syn::Type) -> &syn::Type {
32    if let syn::Type::Reference(type_ref) = ty {
33        return type_ref.elem.as_ref()
34    }
35    ty
36}
37
38pub fn get_type_pass(ty: &syn::Type) -> ValuePassBy {
39    match ty {
40        syn::Type::Reference(reference) => {
41            match &reference.mutability {
42                Some(_) => ValuePassBy::MutRef,
43                None => ValuePassBy::Ref,
44            }
45        },
46        _ => ValuePassBy::Value,
47    }
48}
49
50pub fn get_take_value_code(value: &syn::Ident, pass: ValuePassBy) -> TokenStream {
51    match pass {
52        ValuePassBy::Value => quote!{ #value },
53        ValuePassBy::Ref => quote!{ &#value },
54        ValuePassBy::MutRef => quote!{ &mut #value },
55    }
56}
57
58/// Returns `true` if the input represents a reference type.
59pub fn is_ref(ty: &syn::Type) -> bool {
60    matches!(ty, syn::Type::Reference(_))
61}
62
63pub fn is_mut_ref(ty: &syn::Type) -> bool {
64    if let syn::Type::Reference(ref_) = ty {
65        return ref_.mutability.is_some()
66    }
67    false
68}
69
70/// Extract `syn::Path` from `syn::Type` if it is `Path` variant.
71pub fn path_from_type(src: &syn::Type) -> syn::Result<&syn::Path> {
72    let syn::Type::Path(type_path) = src else {
73        return Err(syn::Error::new(src.span(), format!("TypePath expected. Found type '{}'", type_to_string_fallback(src))))
74    };
75    if let Some(qself) = &type_path.qself {
76        return Err(syn::Error::new(qself.span(), "Qualified self syntax is not supported"))
77    }
78    Ok(&type_path.path)
79}
80
81// Returns a reference to `syn::Ident` of the last path segment,
82// or `None` if the path has no segments.
83pub fn get_ident_of_last_path_segment(src: &syn::Path) -> Option<&syn::Ident> {
84    src.segments.last()
85        .map(|seg| &seg.ident)
86}
87
88// Returns a reference to `syn::Ident` of the last segment in the given `syn::Path`,
89// or `syn::Error` if the path has no segments.
90pub fn get_ident_of_last_path_segment_or_err(src: &syn::Path) -> syn::Result<&syn::Ident> {
91    get_ident_of_last_path_segment(src)
92        .ok_or_else(|| syn::Error::new(src.span(), format!("Failed to get the last segment from path '{}'", path_to_string_fallback(src))))
93}