packages.nim 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2022 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Package related procs.
  10. ##
  11. ## See Also:
  12. ## * `packagehandling` for package path handling
  13. ## * `modulegraphs.getPackage`
  14. ## * `modulegraphs.belongsToStdlib`
  15. import "." / [options, ast, lineinfos, idents, pathutils, msgs]
  16. when defined(nimPreviewSlimSystem):
  17. import std/assertions
  18. proc getPackage*(conf: ConfigRef; cache: IdentCache; fileIdx: FileIndex): PSym =
  19. ## Return a new package symbol.
  20. ##
  21. ## See Also:
  22. ## * `modulegraphs.getPackage`
  23. let
  24. filename = AbsoluteFile toFullPath(conf, fileIdx)
  25. name = getIdent(cache, splitFile(filename).name)
  26. info = newLineInfo(fileIdx, 1, 1)
  27. pkgName = getPackageName(conf, filename.string)
  28. pkgIdent = getIdent(cache, pkgName)
  29. newSym(skPackage, pkgIdent, idGeneratorForPackage(int32(fileIdx)), nil, info)
  30. func getPackageSymbol*(sym: PSym): PSym =
  31. ## Return the owning package symbol.
  32. assert sym != nil
  33. result = sym
  34. while result.kind != skPackage:
  35. result = result.owner
  36. assert result != nil, repr(sym.info)
  37. func getPackageId*(sym: PSym): int =
  38. ## Return the owning package ID.
  39. sym.getPackageSymbol.id
  40. func belongsToProjectPackage*(conf: ConfigRef, sym: PSym): bool =
  41. ## Return whether the symbol belongs to the project's package.
  42. ##
  43. ## See Also:
  44. ## * `modulegraphs.belongsToStdlib`
  45. conf.mainPackageId == sym.getPackageId
  46. func belongsToProjectPackageMaybeNil*(conf: ConfigRef, sym: PSym): bool =
  47. ## Return whether the symbol belongs to the project's package.
  48. ## Returns `false` if `sym` is nil.
  49. ##
  50. ## See Also:
  51. ## * `modulegraphs.belongsToStdlib`
  52. sym != nil and conf.mainPackageId == sym.getPackageId