1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*
* MRustC - Rust Compiler
* - By John Hodge (Mutabah/thePowersGang)
*
* trans/mangling.hpp
* - Name mangling support
*
*
* $D = ! type
* $A = Array
* $S = *-ptr
* $R = &-ptr
* $P = + symbol
* $E = = symbol
* $C = , symbol
* $pL/$pR = Left/right paren
* $aL/$aR = Left/right angle (<>)
*/
#include "mangling.hpp"
#include <hir/type.hpp>
#include <hir/path.hpp>
namespace {
::FmtLambda emit_params(const ::HIR::PathParams& params)
{
return FMT_CB(ss,
if( params.m_types.size() > 0 )
{
ss << "$aL";
for(unsigned int i = 0; i < params.m_types.size(); i ++)
{
if(i != 0) ss << "$C";
ss << Trans_Mangle( params.m_types[i] );
}
ss << "$aR";
}
);
}
}
::FmtLambda Trans_Mangle(const ::HIR::GenericPath& path)
{
return FMT_CB(ss,
ss << "_ZN" << path.m_path.m_crate_name.size() << path.m_path.m_crate_name;
for(const auto& comp : path.m_path.m_components) {
if( comp[0] == '#' )
ss << (comp.size()-1+2) << "$H" << (comp.c_str()+1);
else
ss << comp.size() << comp;
}
ss << emit_params(path.m_params);
);
}
::FmtLambda Trans_Mangle(const ::HIR::Path& path)
{
TU_MATCHA( (path.m_data), (pe),
(Generic,
return Trans_Mangle(pe);
),
(UfcsUnknown,
BUG(Span(), "UfcsUnknown - " << path);
),
(UfcsKnown,
return FMT_CB(ss,
ss << "_ZRK$aL";
ss << Trans_Mangle(*pe.type);
ss << "_as_";
ss << Trans_Mangle(pe.trait);
ss << "$aR";
if( pe.item[0] == '#' )
ss << (pe.item.size()-1+2) << "$H" << (pe.item.c_str()+1);
else
ss << pe.item;
ss << emit_params(pe.params);
);
),
(UfcsInherent,
return FMT_CB(ss,
ss << "_ZRI$aL";
ss << Trans_Mangle(*pe.type);
ss << "$aR";
ss << pe.item;
ss << emit_params(pe.params);
);
)
)
throw "";
}
::FmtLambda Trans_Mangle(const ::HIR::TypeRef& ty)
{
TU_MATCHA( (ty.m_data), (te),
(Infer,
BUG(Span(), "Infer in trans");
),
(Diverge,
return FMT_CB(ss, ss << "$D";);
),
(Primitive,
return FMT_CB(ss, ss << te;);
),
(Path,
return Trans_Mangle(te.path);
),
(Generic,
BUG(Span(), "Generic in trans - " << ty);
),
(TraitObject,
return FMT_CB(ss,
ss << "$pL";
ss << Trans_Mangle(te.m_trait.m_path);
for(const auto& bound : te.m_trait.m_type_bounds) {
ss << "_" << bound.first << "$E" << Trans_Mangle(bound.second);
}
for(const auto& marker : te.m_markers) {
ss << "$P" << Trans_Mangle(marker);
}
ss << "$pR";
);
),
(ErasedType,
BUG(Span(), "ErasedType in trans - " << ty);
),
(Array,
return FMT_CB(ss, ss << "$A" << te.size_val << "_" << Trans_Mangle(*te.inner););
),
(Slice,
return FMT_CB(ss, ss << "$A" << "_" << Trans_Mangle(*te.inner););
),
(Tuple,
return FMT_CB(ss,
ss << "$T";
for(const auto& t : te)
ss << "_" << Trans_Mangle(t);
);
),
(Borrow,
return FMT_CB(ss,
ss << "$R";
switch(te.type)
{
case ::HIR::BorrowType::Shared: ss << "s"; break;
case ::HIR::BorrowType::Unique: ss << "u"; break;
case ::HIR::BorrowType::Owned : ss << "o"; break;
}
ss << "_" << Trans_Mangle(*te.inner);
);
),
(Pointer,
return FMT_CB(ss,
ss << "$S";
switch(te.type)
{
case ::HIR::BorrowType::Shared: ss << "s"; break;
case ::HIR::BorrowType::Unique: ss << "u"; break;
case ::HIR::BorrowType::Owned : ss << "o"; break;
}
ss << "_" << Trans_Mangle(*te.inner);
);
),
(Function,
return FMT_CB(ss,
if(te.m_abi != "Rust")
ss << "extern_" << te.m_abi << "_";
if(te.is_unsafe)
ss << "unsafe_";
ss << "fn_" << te.m_arg_types.size();
for(const auto& ty : te.m_arg_types)
ss << "_" << Trans_Mangle(ty);
ss << "_" << Trans_Mangle(*te.m_rettype);
);
),
(Closure,
BUG(Span(), "Closure during trans - " << ty);
)
)
throw "";
}
|