modulepaths.nim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if n0.kind == nkIdent and n0.ident.s[0] == '/':
  38. let modname = getModuleName(conf, n[2])
  39. # hacky way to implement 'x / y /../ z':
  40. result = getModuleName(conf, n1)
  41. result.add renderTree(n0, {renderNoComments}).replace(" ")
  42. result.add modname
  43. else:
  44. result = ""
  45. of nkPrefix:
  46. when false:
  47. if n[0].kind == nkIdent and n[0].ident.s == "$":
  48. result = lookupPackage(n[1], nil)
  49. else:
  50. discard
  51. # hacky way to implement 'x / y /../ z':
  52. result = renderTree(n, {renderNoComments}).replace(" ")
  53. of nkDotExpr:
  54. localError(conf, n.info, warnDeprecated, "using '.' instead of '/' in import paths is deprecated")
  55. result = renderTree(n, {renderNoComments}).replace(".", "/")
  56. of nkImportAs:
  57. result = getModuleName(conf, n[0])
  58. else:
  59. localError(conf, n.info, "invalid module name: '$1'" % n.renderTree)
  60. result = ""
  61. proc checkModuleName*(conf: ConfigRef; n: PNode; doLocalError=true): FileIndex =
  62. # This returns the full canonical path for a given module import
  63. let modulename = getModuleName(conf, n)
  64. let fullPath = findModule(conf, modulename, toFullPath(conf, n.info))
  65. if fullPath.isEmpty:
  66. if doLocalError:
  67. let m = if modulename.len > 0: modulename else: $n
  68. localError(conf, n.info, "cannot open file: " & m)
  69. result = InvalidFileIdx
  70. else:
  71. result = fileInfoIdx(conf, fullPath)
  72. proc mangleModuleName*(conf: ConfigRef; path: AbsoluteFile): string =
  73. ## Mangle a relative module path to avoid path and symbol collisions.
  74. ##
  75. ## Used by backends that need to generate intermediary files from Nim modules.
  76. ## This is needed because the compiler uses a flat cache file hierarchy.
  77. ##
  78. ## Example:
  79. ## `foo-#head/../bar` becomes `@foo-@hhead@s..@sbar`
  80. "@m" & relativeTo(path, conf.projectPath).string.multiReplace(
  81. {$os.DirSep: "@s", $os.AltSep: "@s", "#": "@h", "@": "@@", ":": "@c"})
  82. proc demangleModuleName*(path: string): string =
  83. ## Demangle a relative module path.
  84. result = path.multiReplace({"@@": "@", "@h": "#", "@s": "/", "@m": "", "@c": ":"})