mangleutils.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import std/strutils
  2. import ast, modulegraphs
  3. proc mangle*(name: string): string =
  4. result = newStringOfCap(name.len)
  5. var start = 0
  6. if name[0] in Digits:
  7. result.add("X" & name[0])
  8. start = 1
  9. var requiresUnderscore = false
  10. template special(x) =
  11. result.add x
  12. requiresUnderscore = true
  13. for i in start..<name.len:
  14. let c = name[i]
  15. case c
  16. of 'a'..'z', '0'..'9', 'A'..'Z':
  17. result.add(c)
  18. of '_':
  19. # we generate names like 'foo_9' for scope disambiguations and so
  20. # disallow this here:
  21. if i > 0 and i < name.len-1 and name[i+1] in Digits:
  22. discard
  23. else:
  24. result.add(c)
  25. of '$': special "dollar"
  26. of '%': special "percent"
  27. of '&': special "amp"
  28. of '^': special "roof"
  29. of '!': special "emark"
  30. of '?': special "qmark"
  31. of '*': special "star"
  32. of '+': special "plus"
  33. of '-': special "minus"
  34. of '/': special "slash"
  35. of '\\': special "backslash"
  36. of '=': special "eq"
  37. of '<': special "lt"
  38. of '>': special "gt"
  39. of '~': special "tilde"
  40. of ':': special "colon"
  41. of '.': special "dot"
  42. of '@': special "at"
  43. of '|': special "bar"
  44. else:
  45. result.add("X" & toHex(ord(c), 2))
  46. requiresUnderscore = true
  47. if requiresUnderscore:
  48. result.add "_"
  49. proc mangleParamExt*(s: PSym): string =
  50. result = "_p"
  51. result.addInt s.position
  52. proc mangleProcNameExt*(graph: ModuleGraph, s: PSym): string =
  53. result = "__"
  54. result.add graph.ifaces[s.itemId.module].uniqueName
  55. result.add "_u"
  56. result.addInt s.itemId.item # s.disamb #