blob: c0f69832840be27548fd7d74b267b0f2b3c3bbd9 [file] [log] [blame]
David Tolnay7953a112020-11-02 12:57:57 -08001use self::ImproperCtype::*;
2use crate::syntax::atom::Atom::{self, *};
3use crate::syntax::{Type, Types};
4use proc_macro2::Ident;
5
6pub enum ImproperCtype<'a> {
7 Definite(bool),
8 Depends(&'a Ident),
9}
10
11impl<'a> Types<'a> {
12 // yes, no, maybe
13 pub fn determine_improper_ctype(&self, ty: &Type) -> ImproperCtype<'a> {
14 match ty {
15 Type::Ident(ident) => {
16 let ident = &ident.rust;
17 if let Some(atom) = Atom::from(ident) {
18 Definite(atom == RustString)
19 } else if let Some(strct) = self.structs.get(ident) {
20 Depends(&strct.name.rust) // iterate to fixed-point
21 } else {
22 Definite(self.rust.contains(ident))
23 }
24 }
25 Type::RustBox(_)
26 | Type::RustVec(_)
27 | Type::Str(_)
28 | Type::Fn(_)
29 | Type::Void(_)
David Tolnay73b72642020-11-25 17:44:05 -080030 | Type::SliceRef(_) => Definite(true),
David Tolnayb3b24a12020-12-01 15:27:43 -080031 Type::UniquePtr(_) | Type::SharedPtr(_) | Type::CxxVector(_) => Definite(false),
David Tolnay7953a112020-11-02 12:57:57 -080032 Type::Ref(ty) => self.determine_improper_ctype(&ty.inner),
Xiangpeng Hao78762352020-11-12 10:24:18 +080033 Type::Array(ty) => self.determine_improper_ctype(&ty.inner),
David Tolnay7953a112020-11-02 12:57:57 -080034 }
35 }
36}