1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 // A Package describes a Go package.
18 fake bool // scope lookup errors are silently dropped if package is fake (internal use only)
19 cgo bool // uses of this package will be rewritten into uses of declarations from _cgo_gotypes.go
20 goVersion string // minimum Go version required for package (by Config.GoVersion, typically from go.mod)
23 // NewPackage returns a new Package for the given package path and name.
24 // The package is not complete and contains no explicit imports.
25 func NewPackage(path, name string) *Package {
26 scope := NewScope(Universe, nopos, nopos, fmt.Sprintf("package %q", path))
27 return &Package{path: path, name: name, scope: scope}
30 // Path returns the package path.
31 func (pkg *Package) Path() string { return pkg.path }
33 // Name returns the package name.
34 func (pkg *Package) Name() string { return pkg.name }
36 // SetName sets the package name.
37 func (pkg *Package) SetName(name string) { pkg.name = name }
39 // GoVersion returns the minimum Go version required by this package.
40 // If the minimum version is unknown, GoVersion returns the empty string.
41 // Individual source files may specify a different minimum Go version,
42 // as reported in the [go/ast.File.GoVersion] field.
43 func (pkg *Package) GoVersion() string { return pkg.goVersion }
45 // Scope returns the (complete or incomplete) package scope
46 // holding the objects declared at package level (TypeNames,
47 // Consts, Vars, and Funcs).
48 // For a nil pkg receiver, Scope returns the Universe scope.
49 func (pkg *Package) Scope() *Scope {
56 // A package is complete if its scope contains (at least) all
57 // exported objects; otherwise it is incomplete.
58 func (pkg *Package) Complete() bool { return pkg.complete }
60 // MarkComplete marks a package as complete.
61 func (pkg *Package) MarkComplete() { pkg.complete = true }
63 // Imports returns the list of packages directly imported by
64 // pkg; the list is in source order.
66 // If pkg was loaded from export data, Imports includes packages that
67 // provide package-level objects referenced by pkg. This may be more or
68 // less than the set of packages directly imported by pkg's source code.
70 // If pkg uses cgo and the FakeImportC configuration option
71 // was enabled, the imports list may contain a fake "C" package.
72 func (pkg *Package) Imports() []*Package { return pkg.imports }
74 // SetImports sets the list of explicitly imported packages to list.
75 // It is the caller's responsibility to make sure list elements are unique.
76 func (pkg *Package) SetImports(list []*Package) { pkg.imports = list }
78 func (pkg *Package) String() string {
79 return fmt.Sprintf("package %s (%q)", pkg.name, pkg.path)