packages.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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, ItemId(module: PackageModuleId, item: 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