modulepaths.nim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2017 Contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. import ast, renderer, msgs, options, idents, lineinfos,
  10. pathutils
  11. import std/[strutils, os]
  12. proc getModuleName*(conf: ConfigRef; n: PNode): string =
  13. # This returns a short relative module name without the nim extension
  14. # e.g. like "system", "importer" or "somepath/module"
  15. # The proc won't perform any checks that the path is actually valid
  16. case n.kind
  17. of nkStrLit, nkRStrLit, nkTripleStrLit:
  18. try:
  19. result = pathSubs(conf, n.strVal, toFullPath(conf, n.info).splitFile().dir)
  20. except ValueError:
  21. localError(conf, n.info, "invalid path: " & n.strVal)
  22. result = n.strVal
  23. of nkIdent:
  24. result = n.ident.s
  25. of nkSym:
  26. result = n.sym.name.s
  27. of nkInfix:
  28. let n0 = n[0]
  29. let n1 = n[1]
  30. when false:
  31. if n1.kind == nkPrefix and n1[0].kind == nkIdent and n1[0].ident.s == "$":
  32. if n0.kind == nkIdent and n0.ident.s == "/":
  33. result = lookupPackage(n1[1], n[2])
  34. else:
  35. localError(n.info, "only '/' supported with $package notation")
  36. result = ""
  37. else:
  38. if n0.kind in nkIdentKinds:
  39. let ident = n0.getPIdent
  40. if ident != nil and ident.s[0] == '/':
  41. let modname = getModuleName(conf, n[2])
  42. # hacky way to implement 'x / y /../ z':
  43. result = getModuleName(conf, n1)
  44. result.add renderTree(n0, {renderNoComments}).replace(" ")
  45. result.add modname
  46. else:
  47. result = ""
  48. else:
  49. result = ""
  50. of nkPrefix:
  51. when false:
  52. if n[0].kind == nkIdent and n[0].ident.s == "$":
  53. result = lookupPackage(n[1], nil)
  54. else:
  55. discard
  56. # hacky way to implement 'x / y /../ z':
  57. result = renderTree(n, {renderNoComments}).replace(" ")
  58. of nkDotExpr:
  59. localError(conf, n.info, warnDeprecated, "using '.' instead of '/' in import paths is deprecated")
  60. result = renderTree(n, {renderNoComments}).replace(".", "/")
  61. of nkImportAs:
  62. result = getModuleName(conf, n[0])
  63. else:
  64. localError(conf, n.info, "invalid module name: '$1'" % n.renderTree)
  65. result = ""
  66. proc checkModuleName*(conf: ConfigRef; n: PNode; doLocalError=true): FileIndex =
  67. # This returns the full canonical path for a given module import
  68. let modulename = getModuleName(conf, n)
  69. let fullPath = findModule(conf, modulename, toFullPath(conf, n.info))
  70. if fullPath.isEmpty:
  71. if doLocalError:
  72. let m = if modulename.len > 0: modulename else: $n
  73. localError(conf, n.info, "cannot open file: " & m)
  74. result = InvalidFileIdx
  75. else:
  76. result = fileInfoIdx(conf, fullPath)
  77. proc mangleModuleName*(conf: ConfigRef; path: AbsoluteFile): string =
  78. ## Mangle a relative module path to avoid path and symbol collisions.
  79. ##
  80. ## Used by backends that need to generate intermediary files from Nim modules.
  81. ## This is needed because the compiler uses a flat cache file hierarchy.
  82. ##
  83. ## Example:
  84. ## `foo-#head/../bar` becomes `@foo-@hhead@s..@sbar`
  85. "@m" & relativeTo(path, conf.projectPath).string.multiReplace(
  86. {$os.DirSep: "@s", $os.AltSep: "@s", "#": "@h", "@": "@@", ":": "@c"})
  87. proc demangleModuleName*(path: string): string =
  88. ## Demangle a relative module path.
  89. result = path.multiReplace({"@@": "@", "@h": "#", "@s": "/", "@m": "", "@c": ":"})