summary refs log tree commit diff stats
path: root/compiler/packages.nim
blob: bb54d61546287dcd96ffc00691a0283fdfc0865b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#
#
#           The Nim Compiler
#        (c) Copyright 2022 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Package related procs.
##
## See Also:
## * `packagehandling` for package path handling
## * `modulegraphs.getPackage`
## * `modulegraphs.belongsToStdlib`

import "." / [options, ast, lineinfos, idents, pathutils, msgs]

when defined(nimPreviewSlimSystem):
  import std/assertions


proc getPackage*(conf: ConfigRef; cache: IdentCache; fileIdx: FileIndex): PSym =
  ## Return a new package symbol.
  ##
  ## See Also:
  ## * `modulegraphs.getPackage`
  let
    filename = AbsoluteFile toFullPath(conf, fileIdx)
    name = getIdent(cache, splitFile(filename).name)
    info = newLineInfo(fileIdx, 1, 1)
    pkgName = getPackageName(conf, filename.string)
    pkgIdent = getIdent(cache, pkgName)
  newSym(skPackage, pkgIdent, idGeneratorForPackage(int32(fileIdx)), nil, info)

func getPackageSymbol*(sym: PSym): PSym =
  ## Return the owning package symbol.
  assert sym != nil
  result = sym
  while result.kind != skPackage:
    result = result.owner
    assert result != nil, repr(sym.info)

func getPackageId*(sym: PSym): int =
  ## Return the owning package ID.
  sym.getPackageSymbol.id

func belongsToProjectPackage*(conf: ConfigRef, sym: PSym): bool =
  ## Return whether the symbol belongs to the project's package.
  ##
  ## See Also:
  ## * `modulegraphs.belongsToStdlib`
  conf.mainPackageId == sym.getPackageId