2019-05-10 14:35:43 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-03-06 14:33:47 -07:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
|
|
|
"go/types"
|
|
|
|
|
2020-05-15 15:13:55 -06:00
|
|
|
"golang.org/x/tools/go/packages"
|
2019-03-06 14:33:47 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-09-09 18:22:42 -06:00
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
errors "golang.org/x/xerrors"
|
2019-03-06 14:33:47 -07:00
|
|
|
)
|
|
|
|
|
2019-05-14 21:04:23 -06:00
|
|
|
// pkg contains the type information needed by the source package.
|
|
|
|
type pkg struct {
|
2020-07-15 15:15:09 -06:00
|
|
|
m *metadata
|
2020-01-30 17:24:33 -07:00
|
|
|
mode source.ParseMode
|
2020-07-21 13:15:06 -06:00
|
|
|
goFiles []*source.ParsedGoFile
|
|
|
|
compiledGoFiles []*source.ParsedGoFile
|
2019-11-20 14:15:00 -07:00
|
|
|
errors []*source.Error
|
|
|
|
imports map[packagePath]*pkg
|
2020-05-15 15:13:55 -06:00
|
|
|
module *packages.Module
|
2020-03-10 08:14:56 -06:00
|
|
|
typeErrors []types.Error
|
2019-11-20 14:15:00 -07:00
|
|
|
types *types.Package
|
|
|
|
typesInfo *types.Info
|
|
|
|
typesSizes types.Sizes
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
|
|
|
|
2020-06-07 19:50:35 -06:00
|
|
|
// Declare explicit types for package paths, names, and IDs to ensure that we
|
|
|
|
// never use an ID where a path belongs, and vice versa. If we confused these,
|
|
|
|
// it would result in confusing errors because package IDs often look like
|
|
|
|
// package paths.
|
|
|
|
type (
|
|
|
|
packageID string
|
|
|
|
packagePath string
|
|
|
|
packageName string
|
|
|
|
)
|
2019-06-11 15:09:26 -06:00
|
|
|
|
2019-12-27 14:44:33 -07:00
|
|
|
// Declare explicit types for files and directories to distinguish between the two.
|
|
|
|
type fileURI span.URI
|
|
|
|
type directoryURI span.URI
|
2020-01-23 23:22:47 -07:00
|
|
|
type viewLoadScope span.URI
|
2019-12-27 14:44:33 -07:00
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) ID() string {
|
2020-07-15 15:15:09 -06:00
|
|
|
return string(p.m.id)
|
2019-06-11 16:06:27 -06:00
|
|
|
}
|
|
|
|
|
2020-06-07 19:50:35 -06:00
|
|
|
func (p *pkg) Name() string {
|
2020-07-15 15:15:09 -06:00
|
|
|
return string(p.m.name)
|
2020-06-07 19:50:35 -06:00
|
|
|
}
|
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) PkgPath() string {
|
2020-07-15 15:15:09 -06:00
|
|
|
return string(p.m.pkgPath)
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
|
|
|
|
2020-07-21 13:15:06 -06:00
|
|
|
func (p *pkg) CompiledGoFiles() []*source.ParsedGoFile {
|
|
|
|
return p.compiledGoFiles
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
|
|
|
|
2020-07-21 13:15:06 -06:00
|
|
|
func (p *pkg) File(uri span.URI) (*source.ParsedGoFile, error) {
|
|
|
|
for _, cgf := range p.compiledGoFiles {
|
|
|
|
if cgf.URI == uri {
|
|
|
|
return cgf, nil
|
2019-11-21 12:54:31 -07:00
|
|
|
}
|
|
|
|
}
|
2020-07-21 13:15:06 -06:00
|
|
|
for _, gf := range p.goFiles {
|
|
|
|
if gf.URI == uri {
|
|
|
|
return gf, nil
|
2019-09-17 09:19:11 -06:00
|
|
|
}
|
|
|
|
}
|
2020-07-22 09:32:32 -06:00
|
|
|
return nil, errors.Errorf("no parsed file for %s in %v", uri, p.m.id)
|
2019-09-17 09:19:11 -06:00
|
|
|
}
|
|
|
|
|
2019-10-24 13:44:41 -06:00
|
|
|
func (p *pkg) GetSyntax() []*ast.File {
|
2019-06-21 15:00:02 -06:00
|
|
|
var syntax []*ast.File
|
2020-07-21 13:15:06 -06:00
|
|
|
for _, pgf := range p.compiledGoFiles {
|
|
|
|
syntax = append(syntax, pgf.File)
|
2019-06-05 15:44:09 -06:00
|
|
|
}
|
|
|
|
return syntax
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
func (p *pkg) GetErrors() []*source.Error {
|
2019-10-02 12:45:55 -06:00
|
|
|
return p.errors
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) GetTypes() *types.Package {
|
|
|
|
return p.types
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) GetTypesInfo() *types.Info {
|
|
|
|
return p.typesInfo
|
2019-03-06 14:33:47 -07:00
|
|
|
}
|
2019-03-11 15:14:55 -06:00
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) GetTypesSizes() types.Sizes {
|
|
|
|
return p.typesSizes
|
2019-03-29 14:39:22 -06:00
|
|
|
}
|
|
|
|
|
2019-10-02 12:45:55 -06:00
|
|
|
func (p *pkg) IsIllTyped() bool {
|
|
|
|
return p.types == nil || p.typesInfo == nil || p.typesSizes == nil
|
2019-03-11 15:14:55 -06:00
|
|
|
}
|
2019-05-10 08:32:25 -06:00
|
|
|
|
2020-01-30 17:24:33 -07:00
|
|
|
func (p *pkg) ForTest() string {
|
2020-07-15 15:15:09 -06:00
|
|
|
return string(p.m.forTest)
|
2020-01-30 17:24:33 -07:00
|
|
|
}
|
|
|
|
|
2019-10-29 16:13:19 -06:00
|
|
|
func (p *pkg) GetImport(pkgPath string) (source.Package, error) {
|
2019-10-02 12:45:55 -06:00
|
|
|
if imp := p.imports[packagePath(pkgPath)]; imp != nil {
|
2019-09-27 11:17:59 -06:00
|
|
|
return imp, nil
|
|
|
|
}
|
|
|
|
// Don't return a nil pointer because that still satisfies the interface.
|
|
|
|
return nil, errors.Errorf("no imported package for %s", pkgPath)
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:15:09 -06:00
|
|
|
func (pkg *pkg) MissingDependencies() []string {
|
|
|
|
var md []string
|
|
|
|
for i := range pkg.m.missingDeps {
|
|
|
|
md = append(md, string(i))
|
|
|
|
}
|
|
|
|
return md
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:13:19 -06:00
|
|
|
func (p *pkg) Imports() []source.Package {
|
|
|
|
var result []source.Package
|
|
|
|
for _, imp := range p.imports {
|
|
|
|
result = append(result, imp)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-15 15:13:55 -06:00
|
|
|
func (p *pkg) Module() *packages.Module {
|
2020-02-11 08:12:40 -07:00
|
|
|
return p.module
|
|
|
|
}
|