| David Tolnay | 7953a11 | 2020-11-02 12:57:57 -0800 | [diff] [blame^] | 1 | use self::ImproperCtype::*; |
| 2 | use crate::syntax::atom::Atom::{self, *}; |
| 3 | use crate::syntax::{Type, Types}; |
| 4 | use proc_macro2::Ident; |
| 5 | |
| 6 | pub enum ImproperCtype<'a> { |
| 7 | Definite(bool), |
| 8 | Depends(&'a Ident), |
| 9 | } |
| 10 | |
| 11 | impl<'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(_) |
| 30 | | Type::Slice(_) |
| 31 | | Type::SliceRefU8(_) => Definite(true), |
| 32 | Type::UniquePtr(_) | Type::CxxVector(_) => Definite(false), |
| 33 | Type::Ref(ty) => self.determine_improper_ctype(&ty.inner), |
| 34 | } |
| 35 | } |
| 36 | } |