depot/third_party/nixpkgs/pkgs/development/compilers/nim/extra-mangling-2.patch
Default email 472aeafc57 Project import generated by Copybara.
GitOrigin-RevId: c31898adf5a8ed202ce5bea9f347b1c6871f32d1
2024-10-04 18:56:33 +02:00

48 lines
1.8 KiB
Diff

diff --git a/compiler/modulepaths.nim b/compiler/modulepaths.nim
index c9e6060e5..acb289498 100644
--- a/compiler/modulepaths.nim
+++ b/compiler/modulepaths.nim
@@ -79,6 +79,13 @@ proc checkModuleName*(conf: ConfigRef; n: PNode; doLocalError=true): FileIndex =
else:
result = fileInfoIdx(conf, fullPath)
+proc rot13(result: var string) =
+ for i, c in result:
+ case c
+ of 'a'..'m', 'A'..'M': result[i] = char(c.uint8 + 13)
+ of 'n'..'z', 'N'..'Z': result[i] = char(c.uint8 - 13)
+ else: discard
+
proc mangleModuleName*(conf: ConfigRef; path: AbsoluteFile): string =
## Mangle a relative module path to avoid path and symbol collisions.
##
@@ -87,9 +94,11 @@ proc mangleModuleName*(conf: ConfigRef; path: AbsoluteFile): string =
##
## Example:
## `foo-#head/../bar` becomes `@foo-@hhead@s..@sbar`
- "@m" & relativeTo(path, conf.projectPath).string.multiReplace(
+ result = "@m" & relativeTo(path, conf.projectPath).string.multiReplace(
{$os.DirSep: "@s", $os.AltSep: "@s", "#": "@h", "@": "@@", ":": "@c"})
+ rot13(result)
proc demangleModuleName*(path: string): string =
## Demangle a relative module path.
result = path.multiReplace({"@@": "@", "@h": "#", "@s": "/", "@m": "", "@c": ":"})
+ rot13(result)
diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim
index 77762d23a..59dd8903a 100644
--- a/compiler/modulegraphs.nim
+++ b/compiler/modulegraphs.nim
@@ -503,7 +503,11 @@ proc uniqueModuleName*(conf: ConfigRef; m: PSym): string =
for i in 0..<trunc:
let c = rel[i]
case c
- of 'a'..'z', '0'..'9':
+ of 'a'..'m':
+ result.add char(c.uint8 + 13)
+ of 'n'..'z':
+ result.add char(c.uint8 - 13)
+ of '0'..'9':
result.add c
of {os.DirSep, os.AltSep}:
result.add 'Z' # because it looks a bit like '/'