blob: a89060d6822b81113cc09c7742a33fa1f0583e16 [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
4pub trait ToMangled {
David Tolnay4ef27432020-04-24 18:38:14 -07005 fn to_mangled(&self, namespace: &Namespace) -> String;
Myron Ahneba35cf2020-02-05 19:41:51 +07006}
7
8impl ToMangled for Type {
David Tolnay4ef27432020-04-24 18:38:14 -07009 fn to_mangled(&self, namespace: &Namespace) -> String {
Myron Ahneba35cf2020-02-05 19:41:51 +070010 match self {
11 Type::Ident(ident) => {
12 let mut instance = String::new();
13 // Do not apply namespace to built-in type
14 let is_user_type = Atom::from(ident).is_none();
15 if is_user_type {
16 for name in namespace {
17 instance += name;
18 instance += "$";
19 }
20 }
21 instance += &ident.to_string();
22 instance
23 }
24 Type::RustBox(ptr) => format!("rust_box${}", ptr.inner.to_mangled(namespace)),
25 Type::RustVec(ptr) => format!("rust_vec${}", ptr.inner.to_mangled(namespace)),
26 Type::UniquePtr(ptr) => format!("std$unique_ptr${}", ptr.inner.to_mangled(namespace)),
David Tolnay4377a9e2020-04-24 15:20:26 -070027 Type::CxxVector(ptr) => format!("std$vector${}", ptr.inner.to_mangled(namespace)),
Myron Ahneba35cf2020-02-05 19:41:51 +070028 _ => unimplemented!(),
29 }
30 }
31}