|
1 | 1 | #include "rust-mangle.h"
|
2 | 2 | #include "fnv-hash.h"
|
| 3 | +#include "rust-base62.h" |
| 4 | +#include <algorithm> |
3 | 5 |
|
4 | 6 | // FIXME: Rename those to legacy_*
|
5 | 7 | static const std::string kMangledSymbolPrefix = "_ZN";
|
@@ -154,6 +156,63 @@ v0_simple_type_prefix (const TyTy::BaseType *ty)
|
154 | 156 | gcc_unreachable ();
|
155 | 157 | }
|
156 | 158 |
|
| 159 | +// Add an underscore-terminated base62 integer to the mangling string. |
| 160 | +// This corresponds to the `<base-62-number>` grammar in the v0 mangling RFC: |
| 161 | +// - 0 is encoded as "_" |
| 162 | +// - any other value is encoded as itself minus one in base 62, followed by "_" |
| 163 | +static void |
| 164 | +v0_add_integer_62 (std::string &mangled, uint64_t x) |
| 165 | +{ |
| 166 | + if (x > 0) |
| 167 | + mangled.append (base62_integer (x - 1)); |
| 168 | + |
| 169 | + mangled.append ("_"); |
| 170 | +} |
| 171 | + |
| 172 | +// Add a tag-prefixed base62 integer to the mangling string when the |
| 173 | +// integer is greater than 0: |
| 174 | +// - 0 is encoded as "" (nothing) |
| 175 | +// - any other value is encoded as <tag> + v0_add_integer_62(itself), that is |
| 176 | +// <tag> + base62(itself - 1) + '_' |
| 177 | +static void |
| 178 | +v0_add_opt_integer_62 (std::string &mangled, std::string tag, uint64_t x) |
| 179 | +{ |
| 180 | + if (x > 0) |
| 181 | + { |
| 182 | + mangled.append (tag); |
| 183 | + v0_add_integer_62 (mangled, x); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +static void |
| 188 | +v0_add_disambiguator (std::string &mangled, uint64_t dis) |
| 189 | +{ |
| 190 | + v0_add_opt_integer_62 (mangled, "s", dis); |
| 191 | +} |
| 192 | + |
| 193 | +// Add an identifier to the mangled string. This corresponds to the |
| 194 | +// `<identifier>` grammar in the v0 mangling RFC. |
| 195 | +static void |
| 196 | +v0_add_identifier (std::string &mangled, const std::string &identifier) |
| 197 | +{ |
| 198 | + // FIXME: gccrs cannot handle unicode identifiers yet, so we never have to |
| 199 | + // create mangling for unicode values for now. However, this is handled |
| 200 | + // by the v0 mangling scheme. The grammar for unicode identifier is contained |
| 201 | + // in <undisambiguated-identifier>, right under the <identifier> one. If the |
| 202 | + // identifier contains unicode values, then an extra "u" needs to be added |
| 203 | + // to the mangling string and `punycode` must be used to encode the |
| 204 | + // characters. |
| 205 | + |
| 206 | + mangled += std::to_string (identifier.size ()); |
| 207 | + |
| 208 | + // If the first character of the identifier is a digit or an underscore, we |
| 209 | + // add an extra underscore |
| 210 | + if (identifier[0] == '_') |
| 211 | + mangled.append ("_"); |
| 212 | + |
| 213 | + mangled.append (identifier); |
| 214 | +} |
| 215 | + |
157 | 216 | static std::string
|
158 | 217 | v0_type_prefix (const TyTy::BaseType *ty)
|
159 | 218 | {
|
@@ -194,7 +253,13 @@ static std::string
|
194 | 253 | v0_mangle_item (const TyTy::BaseType *ty, const Resolver::CanonicalPath &path,
|
195 | 254 | const std::string &crate_name)
|
196 | 255 | {
|
| 256 | + std::string mangled; |
| 257 | + |
| 258 | + // FIXME: Add real algorithm once all pieces are implemented |
197 | 259 | auto ty_prefix = v0_type_prefix (ty);
|
| 260 | + v0_add_identifier (mangled, crate_name); |
| 261 | + v0_add_disambiguator (mangled, 62); |
| 262 | + |
198 | 263 | gcc_unreachable ();
|
199 | 264 | }
|
200 | 265 |
|
|
0 commit comments