modulepaths.nim 3.1 KB

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