Yume
mangle.cpp
Go to the documentation of this file.
1#include "extra/mangle.hpp"
2
3namespace yume::mangle {
4
5[[maybe_unused]] static auto length_encode(const string& name) -> string {
6 return std::to_string(name.length()) + name;
7}
8
9auto mangle_name(Fn& fn) -> string {
10 // TODO(rymiel): static function declarations (i.e. without self) in multiple structs will have identical names.
11 // thus, the recevier should probably be included in the mangled name
12 stringstream ss{};
13 ss << "_Ym.";
14 ss << fn.name();
15 ss << "(";
16 for (const auto& i : llvm::enumerate(fn.arg_types())) {
17 if (i.index() > 0)
18 ss << ",";
19 ss << mangle_name(i.value(), &fn);
20 }
21 ss << ")";
22 // TODO(rymiel): should mangled names even contain the return type...?
23 if (auto ret = fn.ret(); ret.has_value())
24 ss << mangle_name(*ret, &fn);
25
26 return ss.str();
27}
28
29auto mangle_name(ty::Type ast_type, DeclLike parent) -> string {
30 stringstream ss{};
31 if (const auto* ptr_type = ast_type.base_dyn_cast<ty::Ptr>()) {
32 ss << mangle_name(ptr_type->pointee(), parent);
33 if (ptr_type->has_qualifier(Qualifier::Ptr))
34 ss << "*";
35 } else if (const auto* generic_type = ast_type.base_dyn_cast<ty::Generic>()) {
36 auto match = parent.subs()->find_type(generic_type->name());
37 YUME_ASSERT(match.has_value(), "Cannot mangle unsubstituted generic");
38 ss << match->name();
39 } else {
40 ss << ast_type.base_name();
41 }
42
43 if (ast_type.is_mut())
44 ss << "&";
45
46 if (ast_type.is_ref())
47 ss << "^";
48
49 if (ast_type.is_meta())
50 ss << "+";
51
52 return ss.str();
53}
54
55} // namespace yume::mangle
An unsubstituted generic type variable, usually something like T.
Definition: type.hpp:178
A "qualified" type, with a stackable qualifier, i.e. ptr.
Definition: type.hpp:59
A "qualified" type, with a non-stackable qualifier, i.e. mut.
Definition: type_base.hpp:66
static auto length_encode(const string &name) -> string
Definition: mangle.cpp:5
auto mangle_name(Fn &fn) -> string
Definition: mangle.cpp:9
A common base between declarations in the compiler: Fn, Struct and Const. Its value may also be absen...
Definition: vals.hpp:208
A function declaration in the compiler.
Definition: vals.hpp:52
#define YUME_ASSERT(assertion, message)
Definition: util.hpp:81