| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 1 | use crate::syntax::{Atom, Type}; |
| 2 | |
| 3 | pub trait ToMangled { |
| 4 | fn to_mangled(&self, namespace: &Vec<String>) -> String; |
| 5 | } |
| 6 | |
| 7 | impl ToMangled for Type { |
| 8 | fn to_mangled(&self, namespace: &Vec<String>) -> String { |
| 9 | match self { |
| 10 | Type::Ident(ident) => { |
| 11 | let mut instance = String::new(); |
| 12 | // Do not apply namespace to built-in type |
| 13 | let is_user_type = Atom::from(ident).is_none(); |
| 14 | if is_user_type { |
| 15 | for name in namespace { |
| 16 | instance += name; |
| 17 | instance += "$"; |
| 18 | } |
| 19 | } |
| 20 | instance += &ident.to_string(); |
| 21 | instance |
| 22 | } |
| 23 | Type::RustBox(ptr) => format!("rust_box${}", ptr.inner.to_mangled(namespace)), |
| 24 | Type::RustVec(ptr) => format!("rust_vec${}", ptr.inner.to_mangled(namespace)), |
| 25 | Type::UniquePtr(ptr) => format!("std$unique_ptr${}", ptr.inner.to_mangled(namespace)), |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 26 | Type::CxxVector(ptr) => format!("std$vector${}", ptr.inner.to_mangled(namespace)), |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 27 | _ => unimplemented!(), |
| 28 | } |
| 29 | } |
| 30 | } |