1 // Copyright 2019 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 "golang.org/x/mod/module"
12 "golang.org/x/tools/internal/lsp/source"
13 "golang.org/x/tools/internal/span"
14 errors "golang.org/x/xerrors"
17 // pkg contains the type information needed by the source package.
21 goFiles []*source.ParsedGoFile
22 compiledGoFiles []*source.ParsedGoFile
23 diagnostics []*source.Diagnostic
24 imports map[packagePath]*pkg
25 version *module.Version
26 typeErrors []types.Error
29 typesSizes types.Sizes
30 hasListOrParseErrors bool
34 // Declare explicit types for package paths, names, and IDs to ensure that we
35 // never use an ID where a path belongs, and vice versa. If we confused these,
36 // it would result in confusing errors because package IDs often look like
44 // Declare explicit types for files and directories to distinguish between the two.
47 moduleLoadScope string
48 viewLoadScope span.URI
51 func (p *pkg) ID() string {
55 func (p *pkg) Name() string {
56 return string(p.m.name)
59 func (p *pkg) PkgPath() string {
60 return string(p.m.pkgPath)
63 func (p *pkg) CompiledGoFiles() []*source.ParsedGoFile {
64 return p.compiledGoFiles
67 func (p *pkg) File(uri span.URI) (*source.ParsedGoFile, error) {
68 for _, cgf := range p.compiledGoFiles {
73 for _, gf := range p.goFiles {
78 return nil, errors.Errorf("no parsed file for %s in %v", uri, p.m.id)
81 func (p *pkg) GetSyntax() []*ast.File {
82 var syntax []*ast.File
83 for _, pgf := range p.compiledGoFiles {
84 syntax = append(syntax, pgf.File)
89 func (p *pkg) GetTypes() *types.Package {
93 func (p *pkg) GetTypesInfo() *types.Info {
97 func (p *pkg) GetTypesSizes() types.Sizes {
101 func (p *pkg) IsIllTyped() bool {
102 return p.types == nil || p.typesInfo == nil || p.typesSizes == nil
105 func (p *pkg) ForTest() string {
106 return string(p.m.forTest)
109 func (p *pkg) GetImport(pkgPath string) (source.Package, error) {
110 if imp := p.imports[packagePath(pkgPath)]; imp != nil {
113 // Don't return a nil pointer because that still satisfies the interface.
114 return nil, errors.Errorf("no imported package for %s", pkgPath)
117 func (p *pkg) MissingDependencies() []string {
118 // We don't invalidate metadata for import deletions, so check the package
119 // imports via the *types.Package. Only use metadata if p.types is nil.
122 for i := range p.m.missingDeps {
123 md = append(md, string(i))
128 for _, pkg := range p.types.Imports() {
129 if _, ok := p.m.missingDeps[packagePath(pkg.Path())]; ok {
130 md = append(md, pkg.Path())
136 func (p *pkg) Imports() []source.Package {
137 var result []source.Package
138 for _, imp := range p.imports {
139 result = append(result, imp)
144 func (p *pkg) Version() *module.Version {
148 func (p *pkg) HasListOrParseErrors() bool {
149 return p.hasListOrParseErrors
152 func (p *pkg) HasTypeErrors() bool {
153 return p.hasTypeErrors