facts

Imports

Imports #

"bytes"
"encoding/gob"
"fmt"
"go/types"
"io"
"log"
"reflect"
"sort"
"sync"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/types/objectpath"
"go/types"
"golang.org/x/tools/internal/aliases"
"golang.org/x/tools/internal/typesinternal"

Constants & Variables

debug const #

const debug = false

Type Aliases

GetPackageFunc type #

A GetPackageFunc function returns the package denoted by a package path.

type GetPackageFunc func(pkgPath string) *types.Package

Structs

Decoder struct #

A Decoder decodes the facts from the direct imports of the package provided to NewEncoder. A single decoder may be used to decode multiple fact sets (e.g. each for a different set of fact types) for the same package. Each call to Decode returns an independent fact set.

type Decoder struct {
pkg *types.Package
getPackage GetPackageFunc
}

Set struct #

A Set is a set of analysis.Facts. Decode creates a Set of facts by reading from the imports of a given package, and Encode writes out the set. Between these operation, the Import and Export methods will query and update the set. All of Set's methods except String are safe to call concurrently.

type Set struct {
pkg *types.Package
mu sync.Mutex
m map[key]analysis.Fact
}

gobFact struct #

gobFact is the Gob declaration of a serialized fact.

type gobFact struct {
PkgPath string
Object objectpath.Path
Fact analysis.Fact
}

key struct #

type key struct {
pkg *types.Package
obj types.Object
t reflect.Type
}

Functions

AllObjectFacts method #

func (s *Set) AllObjectFacts(filter map[reflect.Type]bool) []analysis.ObjectFact

AllPackageFacts method #

func (s *Set) AllPackageFacts(filter map[reflect.Type]bool) []analysis.PackageFact

Decode method #

Decode decodes all the facts relevant to the analysis of package pkgPath. The read function reads serialized fact data from an external source for one of pkg's direct imports, identified by package path. The empty file is a valid encoding of an empty fact set. It is the caller's responsibility to call gob.Register on all necessary fact types. Concurrent calls to Decode are safe, so long as the [GetPackageFunc] (if any) is also concurrency-safe.

func (d *Decoder) Decode(read func(pkgPath string) ([]byte, error)) (*Set, error)

Encode method #

Encode encodes a set of facts to a memory buffer. It may fail if one of the Facts could not be gob-encoded, but this is a sign of a bug in an Analyzer.

func (s *Set) Encode() []byte

ExportObjectFact method #

ExportObjectFact implements analysis.Pass.ExportObjectFact.

func (s *Set) ExportObjectFact(obj types.Object, fact analysis.Fact)

ExportPackageFact method #

ExportPackageFact implements analysis.Pass.ExportPackageFact.

func (s *Set) ExportPackageFact(fact analysis.Fact)

ImportObjectFact method #

ImportObjectFact implements analysis.Pass.ImportObjectFact.

func (s *Set) ImportObjectFact(obj types.Object, ptr analysis.Fact) bool

ImportPackageFact method #

ImportPackageFact implements analysis.Pass.ImportPackageFact.

func (s *Set) ImportPackageFact(pkg *types.Package, ptr analysis.Fact) bool

NewDecoder function #

NewDecoder returns a fact decoder for the specified package. It uses a brute-force recursive approach to enumerate all objects defined by dependencies of pkg, so that it can learn the set of package paths that may be mentioned in the fact encoding. This does not scale well; use [NewDecoderFunc] where possible.

func NewDecoder(pkg *types.Package) *Decoder

NewDecoderFunc function #

NewDecoderFunc returns a fact decoder for the specified package. It calls the getPackage function for the package path string of each dependency (perhaps indirect) that it encounters in the encoding. If the function returns nil, the fact is discarded. This function is preferred over [NewDecoder] when the client is capable of efficient look-up of packages by package path.

func NewDecoderFunc(pkg *types.Package, getPackage GetPackageFunc) *Decoder

String method #

String is provided only for debugging, and must not be called concurrent with any Import/Export method.

func (s *Set) String() string

importMap function #

importMap computes the import map for a package by traversing the entire exported API each of its imports. This is a workaround for the fact that we cannot access the map used internally by the types.Importer returned by go/importer. The entries in this map are the packages and objects that may be relevant to the current analysis unit. Packages in the map that are only indirectly imported may be incomplete (!pkg.Complete()). This function scales very poorly with packages' transitive object references, which can be more than a million for each package near the top of a large project. (This was a significant contributor to #60621.) TODO(adonovan): opt: compute this information more efficiently by obtaining it from the internals of the gcexportdata decoder.

func importMap(imports []*types.Package) map[string]*types.Package

Generated with Arrow