packages.nim 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. proc getPackage*(conf: ConfigRef; cache: IdentCache; fileIdx: FileIndex): PSym =
  17. ## Return a new package symbol.
  18. ##
  19. ## See Also:
  20. ## * `modulegraphs.getPackage`
  21. let
  22. filename = AbsoluteFile toFullPath(conf, fileIdx)
  23. name = getIdent(cache, splitFile(filename).name)
  24. info = newLineInfo(fileIdx, 1, 1)
  25. pkgName = getPackageName(conf, filename.string)
  26. pkgIdent = getIdent(cache, pkgName)
  27. newSym(skPackage, pkgIdent, ItemId(module: PackageModuleId, item: int32(fileIdx)), nil, info)
  28. func getPackageSymbol*(sym: PSym): PSym =
  29. ## Return the owning package symbol.
  30. assert sym != nil
  31. result = sym
  32. while result.kind != skPackage:
  33. result = result.owner
  34. assert result != nil, repr(sym.info)
  35. func getPackageId*(sym: PSym): int =
  36. ## Return the owning package ID.
  37. sym.getPackageSymbol.id
  38. func belongsToProjectPackage*(conf: ConfigRef, sym: PSym): bool =
  39. ## Return whether the symbol belongs to the project's package.
  40. ##
  41. ## See Also:
  42. ## * `modulegraphs.belongsToStdlib`
  43. conf.mainPackageId == sym.getPackageId