mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-29 09:24:36 +00:00
* Refactor and doc package handling, module name mangling * Consolidate, de-duplicate and extend package handling * Alter how duplicate module names of a package are handled * Alter how module names are mangled * Fix crash when another package is named 'stdlib' (test case added) * Doc what defines a package in the manual Modules with duplicate names within a package used to be given 'fake' packages to resolve conflicts. That prevented the ability to discern if a module belonged to the current project package or a foreign package. They now have the proper package owner and the names are mangled in a consistent manner to prevent codegen clashes. All module names are now mangled the same. Stdlib was treated special before, but now it is same as any other package. This fixes a crash when a foreign package is named 'stdlib'. Module mangling is altered for both file paths and symbols used by the backends. Removed an unused module name to package mapping that may have been intended for IC. The mapping was removed because it wasn't being used and was complicating the issue of package modules with duplicate names not having the proper package owner assigned. * Fix some tests * Refactor `packagehandling` * Remove `packagehandling.withPackageName` and its uses * Move module path mangling from `packagehandling` to `modulepaths` * Move `options.toRodFile` to `ic` to break import cycle * Changed import style to match preferred style Co-authored-by: quantimnot <quantimnot@users.noreply.github.com>
50 lines
1.4 KiB
Nim
50 lines
1.4 KiB
Nim
#
|
|
#
|
|
# 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]
|
|
|
|
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, ItemId(module: PackageModuleId, item: 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
|