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