blob: f52aa433fab3c80a31caea8237ac3c82b6a1b7c8 [file] [log] [blame]
David Tolnay4ef27432020-04-24 18:38:14 -07001use crate::syntax::namespace::Namespace;
Myron Ahneba35cf2020-02-05 19:41:51 +07002use crate::syntax::{Atom, Type};
3
David Tolnayf12e9832020-04-24 18:46:44 -07004pub 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 Ahneba35cf2020-02-05 19:41:51 +070014 }
Myron Ahneba35cf2020-02-05 19:41:51 +070015 }
David Tolnayf12e9832020-04-24 18:46:44 -070016 instance += &ident.to_string();
17 instance
Myron Ahneba35cf2020-02-05 19:41:51 +070018 }
David Tolnayf12e9832020-04-24 18:46:44 -070019 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 Ahneba35cf2020-02-05 19:41:51 +070024 }
25}