Debian Codegraph @

gopls/internal/cache/package.go

raw symbols SCIP
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.
4
5 package cache
6
7 import (
8 "fmt"
9 "go/ast"
10 "go/scanner"
11 "go/token"
12 "go/types"
13 "slices"
14 "sync"
15
16 "golang.org/x/tools/gopls/internal/cache/metadata"
17 "golang.org/x/tools/gopls/internal/cache/methodsets"
18 "golang.org/x/tools/gopls/internal/cache/parsego"
19 "golang.org/x/tools/gopls/internal/cache/testfuncs"
20 "golang.org/x/tools/gopls/internal/cache/xrefs"
21 "golang.org/x/tools/gopls/internal/protocol"
22 "golang.org/x/tools/gopls/internal/util/safetoken"
23 )
24
25 // Convenient aliases for very heavily used types.
26 type (
27 PackageID = metadata.PackageID
28 PackagePath = metadata.PackagePath
29 PackageName = metadata.PackageName
30 ImportPath = metadata.ImportPath
31 )
32
33 // A Package is the union of package metadata and type checking results.
34 //
35 // TODO(rfindley): for now, we do not persist the post-processing of
36 // loadDiagnostics, because the value of the snapshot.packages map is just the
37 // package handle. Fix this.
38 type Package struct {
39 metadata *metadata.Package
40 loadDiagnostics []*Diagnostic
41 pkg *syntaxPackage
42 }
43
44 // syntaxPackage contains parse trees and type information for a package.
45 type syntaxPackage struct {
46 // -- identifiers --
47 id PackageID
48
49 // -- outputs --
50 fset *token.FileSet // for now, same as the snapshot's FileSet
51 goFiles []*parsego.File
52 compiledGoFiles []*parsego.File
53 diagnostics []*Diagnostic
54 parseErrors []scanner.ErrorList
55 typeErrors []types.Error
56 types *types.Package
57 typesInfo *types.Info
58 typesSizes types.Sizes
59 importMap map[PackagePath]*types.Package
60
61 xrefsOnce sync.Once
62 _xrefs *xrefs.Index // only used by the xrefs method
63
64 methodsetsOnce sync.Once
65 _methodsets *methodsets.Index // only used by the methodsets method
66
67 testsOnce sync.Once
68 _tests *testfuncs.Index // only used by the tests method
69 }
70
71 func (p *syntaxPackage) xrefs() *xrefs.Index {
72 p.xrefsOnce.Do(func() {
73 p._xrefs = xrefs.NewIndex(p.compiledGoFiles, p.types, p.typesInfo)
74 })
75 return p._xrefs
76 }
77
78 func (p *syntaxPackage) methodsets() *methodsets.Index {
79 p.methodsetsOnce.Do(func() {
80 p._methodsets = methodsets.NewIndex(p.fset, p.types)
81 })
82 return p._methodsets
83 }
84
85 func (p *syntaxPackage) tests() *testfuncs.Index {
86 p.testsOnce.Do(func() {
87 p._tests = testfuncs.NewIndex(p.compiledGoFiles, p.typesInfo)
88 })
89 return p._tests
90 }
91
92 // hasFixedFiles reports whether there are any 'fixed' compiled go files in the
93 // package.
94 //
95 // Intended to be used to refine bug reports.
96 func (p *syntaxPackage) hasFixedFiles() bool {
97 return slices.ContainsFunc(p.compiledGoFiles, (*parsego.File).Fixed)
98 }
99
100 func (p *Package) String() string { return string(p.metadata.ID) }
101
102 func (p *Package) Metadata() *metadata.Package { return p.metadata }
103
104 // A loadScope defines a package loading scope for use with go/packages.
105 //
106 // TODO(rfindley): move this to load.go.
107 type loadScope interface {
108 aScope()
109 }
110
111 // TODO(rfindley): move to load.go
112 type (
113 fileLoadScope protocol.DocumentURI // load packages containing a file (including command-line-arguments)
114 packageLoadScope string // load a specific package (the value is its PackageID)
115 moduleLoadScope struct {
116 dir string // dir containing the go.mod file
117 modulePath string // parsed module path
118 }
119 viewLoadScope struct{} // load the workspace
120 )
121
122 // Implement the loadScope interface.
123 func (fileLoadScope) aScope() {}
124 func (packageLoadScope) aScope() {}
125 func (moduleLoadScope) aScope() {}
126 func (viewLoadScope) aScope() {}
127
128 func (p *Package) CompiledGoFiles() []*parsego.File {
129 return p.pkg.compiledGoFiles
130 }
131
132 func (p *Package) File(uri protocol.DocumentURI) (*parsego.File, error) {
133 return p.pkg.File(uri)
134 }
135
136 // FileEnclosing returns the file of pkg that encloses the specified position,
137 // which must be mapped by p.FileSet().
138 func (p *Package) FileEnclosing(pos token.Pos) (*parsego.File, error) {
139 for _, files := range [...][]*parsego.File{p.pkg.compiledGoFiles, p.pkg.goFiles} {
140 for _, pgf := range files {
141 if pgf.File.FileStart <= pos && pos <= pgf.File.FileEnd {
142 return pgf, nil
143 }
144 }
145 }
146 return nil, fmt.Errorf("no parsed file for position %d (%s) in %v",
147 pos, safetoken.StartPosition(p.FileSet(), pos), p.pkg.id)
148 }
149
150 func (pkg *syntaxPackage) File(uri protocol.DocumentURI) (*parsego.File, error) {
151 for _, files := range [...][]*parsego.File{pkg.compiledGoFiles, pkg.goFiles} {
152 for _, pgf := range files {
153 if pgf.URI == uri {
154 return pgf, nil
155 }
156 }
157 }
158 return nil, fmt.Errorf("no parsed file for %s in %v", uri, pkg.id)
159 }
160
161 // Syntax returns parsed compiled Go files contained in this package.
162 func (p *Package) Syntax() []*ast.File {
163 var syntax []*ast.File
164 for _, pgf := range p.pkg.compiledGoFiles {
165 syntax = append(syntax, pgf.File)
166 }
167 return syntax
168 }
169
170 // FileSet returns the FileSet describing this package's positions.
171 //
172 // The returned FileSet is guaranteed to describe all Syntax, but may also
173 // describe additional files.
174 func (p *Package) FileSet() *token.FileSet {
175 return p.pkg.fset
176 }
177
178 // Types returns the type checked go/types.Package.
179 func (p *Package) Types() *types.Package {
180 return p.pkg.types
181 }
182
183 // TypesInfo returns the go/types.Info annotating the Syntax of this package
184 // with type information.
185 //
186 // All fields in the resulting Info are populated.
187 func (p *Package) TypesInfo() *types.Info {
188 return p.pkg.typesInfo
189 }
190
191 // TypesSizes returns the sizing function used for types in this package.
192 func (p *Package) TypesSizes() types.Sizes {
193 return p.pkg.typesSizes
194 }
195
196 // DependencyTypes returns the type checker's symbol for the specified
197 // package. It returns nil if path is not among the transitive
198 // dependencies of p, or if no symbols from that package were
199 // referenced during the type-checking of p.
200 func (p *Package) DependencyTypes(path PackagePath) *types.Package {
201 return p.pkg.importMap[path]
202 }
203
204 // ParseErrors returns a slice containing all non-empty parse errors produces
205 // while parsing p.Syntax, or nil if the package contains no parse errors.
206 func (p *Package) ParseErrors() []scanner.ErrorList {
207 return p.pkg.parseErrors
208 }
209
210 // TypeErrors returns the go/types.Errors produced during type checking this
211 // package, if any.
212 func (p *Package) TypeErrors() []types.Error {
213 return p.pkg.typeErrors
214 }