| chigraph master Systems programming language written for beginners in LLVM

NameMangler.cpp
Go to the documentation of this file.
1
3 #include "chi/NameMangler.hpp"
4
5 #include <cassert>
6 #include <vector>
7
8 namespace chi {
9 std::string mangleFunctionName(std::string modName, const std::string& name) {
10 // if it is the special main module, then just return chigraph_main
11 if ((modName.substr(modName.rfind('/') + 1, modName.rfind('.')) == "main" ||
12 modName.substr(modName.rfind('\\') + 1, modName.rfind('.')) == "main") &&
13 name == "main") {
14 return "chigraph_main";
15 }
16
17 // escape characters
18 // escape _
19 size_t id = modName.find('_');
20 while (id != std::string::npos) {
21 modName.replace(id, 1, "__");
22
23 id = modName.find('_', id + 2);
24 }
25
26 // escape /
27 id = modName.find('/');
28 while (id != std::string::npos) {
29 modName.replace(id, 1, "_s");
30
31 id = modName.find('/', id);
32 }
33
34 // escape .
35 id = modName.find('.');
36 while (id != std::string::npos) {
37 modName.replace(id, 1, "_d");
38
39 id = modName.find('.', id);
40 }
41
42 return modName + "_m" + name;
43 }
44
45 std::pair<std::string, std::string> unmangleFunctionName(std::string mangled) {
46 if (mangled == "main") { return {"main", "main"}; }
47
48 size_t splitter = mangled.find("_m");
49 std::string modName = mangled.substr(0, splitter);
50 std::string typeName = mangled.substr(splitter + 2);
51
52 size_t id = 0;
53 while ((id = modName.find('_', static_cast<size_t>(id))) != std::string::npos &&
54 modName.size() >= id) {
55 switch (modName[static_cast<size_t>(id + 1)]) {
56 case '_': modName.replace(id, 2, "_"); break;
57 case 's': modName.replace(id, 2, "/"); break;
58 case 'd': modName.replace(id, 2, "."); break;
59 default:
60 assert(false); // TODO: error handling
61 }
62
63 ++id;
64 }
65
66 return {modName, typeName};
67 }
68 } // namespace chi
chi::mangleFunctionName
std::string mangleFunctionName(std::string fullModuleName, const std::string &name)
Mangle a function name.
Definition: NameMangler.cpp:9
NameMangler.hpp
Definitions for mangling functions.
chi::unmangleFunctionName
std::pair< std::string, std::string > unmangleFunctionName(std::string mangled)
Unmangle a function name.
Definition: NameMangler.cpp:45
chi
The namespace where chigraph lives.
Definition: BitcodeParser.hpp:12