blob: 56e8b73d22a1708ba093698cf2cc7f02694b97ea [file] [log] [blame]
Myron Ahneba35cf2020-02-05 19:41:51 +07001use crate::syntax::{Atom, Type};
2
3pub trait ToMangled {
4 fn to_mangled(&self, namespace: &Vec<String>) -> String;
5}
6
7impl 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)),
26 Type::Vector(ptr) => format!("std$vector${}", ptr.inner.to_mangled(namespace)),
27 _ => unimplemented!(),
28 }
29 }
30}