summaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/tools/go
diff options
context:
space:
mode:
authorLibravatar Terin Stock <terinjokes@gmail.com>2025-03-09 17:47:56 +0100
committerLibravatar Terin Stock <terinjokes@gmail.com>2025-03-10 01:59:49 +0100
commit3ac1ee16f377d31a0fb80c8dae28b6239ac4229e (patch)
treef61faa581feaaeaba2542b9f2b8234a590684413 /vendor/golang.org/x/tools/go
parent[chore] update URLs to forked source (diff)
downloadgotosocial-3ac1ee16f377d31a0fb80c8dae28b6239ac4229e.tar.xz
[chore] remove vendor
Diffstat (limited to 'vendor/golang.org/x/tools/go')
-rw-r--r--vendor/golang.org/x/tools/go/ast/astutil/enclosing.go654
-rw-r--r--vendor/golang.org/x/tools/go/ast/astutil/imports.go490
-rw-r--r--vendor/golang.org/x/tools/go/ast/astutil/rewrite.go486
-rw-r--r--vendor/golang.org/x/tools/go/ast/astutil/util.go11
-rw-r--r--vendor/golang.org/x/tools/go/gcexportdata/gcexportdata.go239
-rw-r--r--vendor/golang.org/x/tools/go/gcexportdata/importer.go75
-rw-r--r--vendor/golang.org/x/tools/go/packages/doc.go251
-rw-r--r--vendor/golang.org/x/tools/go/packages/external.go153
-rw-r--r--vendor/golang.org/x/tools/go/packages/golist.go1081
-rw-r--r--vendor/golang.org/x/tools/go/packages/golist_overlay.go83
-rw-r--r--vendor/golang.org/x/tools/go/packages/loadmode_string.go55
-rw-r--r--vendor/golang.org/x/tools/go/packages/packages.go1564
-rw-r--r--vendor/golang.org/x/tools/go/packages/visit.go68
-rw-r--r--vendor/golang.org/x/tools/go/types/objectpath/objectpath.go817
-rw-r--r--vendor/golang.org/x/tools/go/types/typeutil/callee.go68
-rw-r--r--vendor/golang.org/x/tools/go/types/typeutil/imports.go30
-rw-r--r--vendor/golang.org/x/tools/go/types/typeutil/map.go517
-rw-r--r--vendor/golang.org/x/tools/go/types/typeutil/methodsetcache.go71
-rw-r--r--vendor/golang.org/x/tools/go/types/typeutil/ui.go53
19 files changed, 0 insertions, 6766 deletions
diff --git a/vendor/golang.org/x/tools/go/ast/astutil/enclosing.go b/vendor/golang.org/x/tools/go/ast/astutil/enclosing.go
deleted file mode 100644
index 6e34df461..000000000
--- a/vendor/golang.org/x/tools/go/ast/astutil/enclosing.go
+++ /dev/null
@@ -1,654 +0,0 @@
-// Copyright 2013 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.
-
-package astutil
-
-// This file defines utilities for working with source positions.
-
-import (
- "fmt"
- "go/ast"
- "go/token"
- "sort"
-)
-
-// PathEnclosingInterval returns the node that encloses the source
-// interval [start, end), and all its ancestors up to the AST root.
-//
-// The definition of "enclosing" used by this function considers
-// additional whitespace abutting a node to be enclosed by it.
-// In this example:
-//
-// z := x + y // add them
-// <-A->
-// <----B----->
-//
-// the ast.BinaryExpr(+) node is considered to enclose interval B
-// even though its [Pos()..End()) is actually only interval A.
-// This behaviour makes user interfaces more tolerant of imperfect
-// input.
-//
-// This function treats tokens as nodes, though they are not included
-// in the result. e.g. PathEnclosingInterval("+") returns the
-// enclosing ast.BinaryExpr("x + y").
-//
-// If start==end, the 1-char interval following start is used instead.
-//
-// The 'exact' result is true if the interval contains only path[0]
-// and perhaps some adjacent whitespace. It is false if the interval
-// overlaps multiple children of path[0], or if it contains only
-// interior whitespace of path[0].
-// In this example:
-//
-// z := x + y // add them
-// <--C--> <---E-->
-// ^
-// D
-//
-// intervals C, D and E are inexact. C is contained by the
-// z-assignment statement, because it spans three of its children (:=,
-// x, +). So too is the 1-char interval D, because it contains only
-// interior whitespace of the assignment. E is considered interior
-// whitespace of the BlockStmt containing the assignment.
-//
-// The resulting path is never empty; it always contains at least the
-// 'root' *ast.File. Ideally PathEnclosingInterval would reject
-// intervals that lie wholly or partially outside the range of the
-// file, but unfortunately ast.File records only the token.Pos of
-// the 'package' keyword, but not of the start of the file itself.
-func PathEnclosingInterval(root *ast.File, start, end token.Pos) (path []ast.Node, exact bool) {
- // fmt.Printf("EnclosingInterval %d %d\n", start, end) // debugging
-
- // Precondition: node.[Pos..End) and adjoining whitespace contain [start, end).
- var visit func(node ast.Node) bool
- visit = func(node ast.Node) bool {
- path = append(path, node)
-
- nodePos := node.Pos()
- nodeEnd := node.End()
-
- // fmt.Printf("visit(%T, %d, %d)\n", node, nodePos, nodeEnd) // debugging
-
- // Intersect [start, end) with interval of node.
- if start < nodePos {
- start = nodePos
- }
- if end > nodeEnd {
- end = nodeEnd
- }
-
- // Find sole child that contains [start, end).
- children := childrenOf(node)
- l := len(children)
- for i, child := range children {
- // [childPos, childEnd) is unaugmented interval of child.
- childPos := child.Pos()
- childEnd := child.End()
-
- // [augPos, augEnd) is whitespace-augmented interval of child.
- augPos := childPos
- augEnd := childEnd
- if i > 0 {
- augPos = children[i-1].End() // start of preceding whitespace
- }
- if i < l-1 {
- nextChildPos := children[i+1].Pos()
- // Does [start, end) lie between child and next child?
- if start >= augEnd && end <= nextChildPos {
- return false // inexact match
- }
- augEnd = nextChildPos // end of following whitespace
- }
-
- // fmt.Printf("\tchild %d: [%d..%d)\tcontains interval [%d..%d)?\n",
- // i, augPos, augEnd, start, end) // debugging
-
- // Does augmented child strictly contain [start, end)?
- if augPos <= start && end <= augEnd {
- if is[tokenNode](child) {
- return true
- }
-
- // childrenOf elides the FuncType node beneath FuncDecl.
- // Add it back here for TypeParams, Params, Results,
- // all FieldLists). But we don't add it back for the "func" token
- // even though it is is the tree at FuncDecl.Type.Func.
- if decl, ok := node.(*ast.FuncDecl); ok {
- if fields, ok := child.(*ast.FieldList); ok && fields != decl.Recv {
- path = append(path, decl.Type)
- }
- }
-
- return visit(child)
- }
-
- // Does [start, end) overlap multiple children?
- // i.e. left-augmented child contains start
- // but LR-augmented child does not contain end.
- if start < childEnd && end > augEnd {
- break
- }
- }
-
- // No single child contained [start, end),
- // so node is the result. Is it exact?
-
- // (It's tempting to put this condition before the
- // child loop, but it gives the wrong result in the
- // case where a node (e.g. ExprStmt) and its sole
- // child have equal intervals.)
- if start == nodePos && end == nodeEnd {
- return true // exact match
- }
-
- return false // inexact: overlaps multiple children
- }
-
- // Ensure [start,end) is nondecreasing.
- if start > end {
- start, end = end, start
- }
-
- if start < root.End() && end > root.Pos() {
- if start == end {
- end = start + 1 // empty interval => interval of size 1
- }
- exact = visit(root)
-
- // Reverse the path:
- for i, l := 0, len(path); i < l/2; i++ {
- path[i], path[l-1-i] = path[l-1-i], path[i]
- }
- } else {
- // Selection lies within whitespace preceding the
- // first (or following the last) declaration in the file.
- // The result nonetheless always includes the ast.File.
- path = append(path, root)
- }
-
- return
-}
-
-// tokenNode is a dummy implementation of ast.Node for a single token.
-// They are used transiently by PathEnclosingInterval but never escape
-// this package.
-type tokenNode struct {
- pos token.Pos
- end token.Pos
-}
-
-func (n tokenNode) Pos() token.Pos {
- return n.pos
-}
-
-func (n tokenNode) End() token.Pos {
- return n.end
-}
-
-func tok(pos token.Pos, len int) ast.Node {
- return tokenNode{pos, pos + token.Pos(len)}
-}
-
-// childrenOf returns the direct non-nil children of ast.Node n.
-// It may include fake ast.Node implementations for bare tokens.
-// it is not safe to call (e.g.) ast.Walk on such nodes.
-func childrenOf(n ast.Node) []ast.Node {
- var children []ast.Node
-
- // First add nodes for all true subtrees.
- ast.Inspect(n, func(node ast.Node) bool {
- if node == n { // push n
- return true // recur
- }
- if node != nil { // push child
- children = append(children, node)
- }
- return false // no recursion
- })
-
- // Then add fake Nodes for bare tokens.
- switch n := n.(type) {
- case *ast.ArrayType:
- children = append(children,
- tok(n.Lbrack, len("[")),
- tok(n.Elt.End(), len("]")))
-
- case *ast.AssignStmt:
- children = append(children,
- tok(n.TokPos, len(n.Tok.String())))
-
- case *ast.BasicLit:
- children = append(children,
- tok(n.ValuePos, len(n.Value)))
-
- case *ast.BinaryExpr:
- children = append(children, tok(n.OpPos, len(n.Op.String())))
-
- case *ast.BlockStmt:
- children = append(children,
- tok(n.Lbrace, len("{")),
- tok(n.Rbrace, len("}")))
-
- case *ast.BranchStmt:
- children = append(children,
- tok(n.TokPos, len(n.Tok.String())))
-
- case *ast.CallExpr:
- children = append(children,
- tok(n.Lparen, len("(")),
- tok(n.Rparen, len(")")))
- if n.Ellipsis != 0 {
- children = append(children, tok(n.Ellipsis, len("...")))
- }
-
- case *ast.CaseClause:
- if n.List == nil {
- children = append(children,
- tok(n.Case, len("default")))
- } else {
- children = append(children,
- tok(n.Case, len("case")))
- }
- children = append(children, tok(n.Colon, len(":")))
-
- case *ast.ChanType:
- switch n.Dir {
- case ast.RECV:
- children = append(children, tok(n.Begin, len("<-chan")))
- case ast.SEND:
- children = append(children, tok(n.Begin, len("chan<-")))
- case ast.RECV | ast.SEND:
- children = append(children, tok(n.Begin, len("chan")))
- }
-
- case *ast.CommClause:
- if n.Comm == nil {
- children = append(children,
- tok(n.Case, len("default")))
- } else {
- children = append(children,
- tok(n.Case, len("case")))
- }
- children = append(children, tok(n.Colon, len(":")))
-
- case *ast.Comment:
- // nop
-
- case *ast.CommentGroup:
- // nop
-
- case *ast.CompositeLit:
- children = append(children,
- tok(n.Lbrace, len("{")),
- tok(n.Rbrace, len("{")))
-
- case *ast.DeclStmt:
- // nop
-
- case *ast.DeferStmt:
- children = append(children,
- tok(n.Defer, len("defer")))
-
- case *ast.Ellipsis:
- children = append(children,
- tok(n.Ellipsis, len("...")))
-
- case *ast.EmptyStmt:
- // nop
-
- case *ast.ExprStmt:
- // nop
-
- case *ast.Field:
- // TODO(adonovan): Field.{Doc,Comment,Tag}?
-
- case *ast.FieldList:
- children = append(children,
- tok(n.Opening, len("(")), // or len("[")
- tok(n.Closing, len(")"))) // or len("]")
-
- case *ast.File:
- // TODO test: Doc
- children = append(children,
- tok(n.Package, len("package")))
-
- case *ast.ForStmt:
- children = append(children,
- tok(n.For, len("for")))
-
- case *ast.FuncDecl:
- // TODO(adonovan): FuncDecl.Comment?
-
- // Uniquely, FuncDecl breaks the invariant that
- // preorder traversal yields tokens in lexical order:
- // in fact, FuncDecl.Recv precedes FuncDecl.Type.Func.
- //
- // As a workaround, we inline the case for FuncType
- // here and order things correctly.
- // We also need to insert the elided FuncType just
- // before the 'visit' recursion.
- //
- children = nil // discard ast.Walk(FuncDecl) info subtrees
- children = append(children, tok(n.Type.Func, len("func")))
- if n.Recv != nil {
- children = append(children, n.Recv)
- }
- children = append(children, n.Name)
- if tparams := n.Type.TypeParams; tparams != nil {
- children = append(children, tparams)
- }
- if n.Type.Params != nil {
- children = append(children, n.Type.Params)
- }
- if n.Type.Results != nil {
- children = append(children, n.Type.Results)
- }
- if n.Body != nil {
- children = append(children, n.Body)
- }
-
- case *ast.FuncLit:
- // nop
-
- case *ast.FuncType:
- if n.Func != 0 {
- children = append(children,
- tok(n.Func, len("func")))
- }
-
- case *ast.GenDecl:
- children = append(children,
- tok(n.TokPos, len(n.Tok.String())))
- if n.Lparen != 0 {
- children = append(children,
- tok(n.Lparen, len("(")),
- tok(n.Rparen, len(")")))
- }
-
- case *ast.GoStmt:
- children = append(children,
- tok(n.Go, len("go")))
-
- case *ast.Ident:
- children = append(children,
- tok(n.NamePos, len(n.Name)))
-
- case *ast.IfStmt:
- children = append(children,
- tok(n.If, len("if")))
-
- case *ast.ImportSpec:
- // TODO(adonovan): ImportSpec.{Doc,EndPos}?
-
- case *ast.IncDecStmt:
- children = append(children,
- tok(n.TokPos, len(n.Tok.String())))
-
- case *ast.IndexExpr:
- children = append(children,
- tok(n.Lbrack, len("[")),
- tok(n.Rbrack, len("]")))
-
- case *ast.IndexListExpr:
- children = append(children,
- tok(n.Lbrack, len("[")),
- tok(n.Rbrack, len("]")))
-
- case *ast.InterfaceType:
- children = append(children,
- tok(n.Interface, len("interface")))
-
- case *ast.KeyValueExpr:
- children = append(children,
- tok(n.Colon, len(":")))
-
- case *ast.LabeledStmt:
- children = append(children,
- tok(n.Colon, len(":")))
-
- case *ast.MapType:
- children = append(children,
- tok(n.Map, len("map")))
-
- case *ast.ParenExpr:
- children = append(children,
- tok(n.Lparen, len("(")),
- tok(n.Rparen, len(")")))
-
- case *ast.RangeStmt:
- children = append(children,
- tok(n.For, len("for")),
- tok(n.TokPos, len(n.Tok.String())))
-
- case *ast.ReturnStmt:
- children = append(children,
- tok(n.Return, len("return")))
-
- case *ast.SelectStmt:
- children = append(children,
- tok(n.Select, len("select")))
-
- case *ast.SelectorExpr:
- // nop
-
- case *ast.SendStmt:
- children = append(children,
- tok(n.Arrow, len("<-")))
-
- case *ast.SliceExpr:
- children = append(children,
- tok(n.Lbrack, len("[")),
- tok(n.Rbrack, len("]")))
-
- case *ast.StarExpr:
- children = append(children, tok(n.Star, len("*")))
-
- case *ast.StructType:
- children = append(children, tok(n.Struct, len("struct")))
-
- case *ast.SwitchStmt:
- children = append(children, tok(n.Switch, len("switch")))
-
- case *ast.TypeAssertExpr:
- children = append(children,
- tok(n.Lparen-1, len(".")),
- tok(n.Lparen, len("(")),
- tok(n.Rparen, len(")")))
-
- case *ast.TypeSpec:
- // TODO(adonovan): TypeSpec.{Doc,Comment}?
-
- case *ast.TypeSwitchStmt:
- children = append(children, tok(n.Switch, len("switch")))
-
- case *ast.UnaryExpr:
- children = append(children, tok(n.OpPos, len(n.Op.String())))
-
- case *ast.ValueSpec:
- // TODO(adonovan): ValueSpec.{Doc,Comment}?
-
- case *ast.BadDecl, *ast.BadExpr, *ast.BadStmt:
- // nop
- }
-
- // TODO(adonovan): opt: merge the logic of ast.Inspect() into
- // the switch above so we can make interleaved callbacks for
- // both Nodes and Tokens in the right order and avoid the need
- // to sort.
- sort.Sort(byPos(children))
-
- return children
-}
-
-type byPos []ast.Node
-
-func (sl byPos) Len() int {
- return len(sl)
-}
-func (sl byPos) Less(i, j int) bool {
- return sl[i].Pos() < sl[j].Pos()
-}
-func (sl byPos) Swap(i, j int) {
- sl[i], sl[j] = sl[j], sl[i]
-}
-
-// NodeDescription returns a description of the concrete type of n suitable
-// for a user interface.
-//
-// TODO(adonovan): in some cases (e.g. Field, FieldList, Ident,
-// StarExpr) we could be much more specific given the path to the AST
-// root. Perhaps we should do that.
-func NodeDescription(n ast.Node) string {
- switch n := n.(type) {
- case *ast.ArrayType:
- return "array type"
- case *ast.AssignStmt:
- return "assignment"
- case *ast.BadDecl:
- return "bad declaration"
- case *ast.BadExpr:
- return "bad expression"
- case *ast.BadStmt:
- return "bad statement"
- case *ast.BasicLit:
- return "basic literal"
- case *ast.BinaryExpr:
- return fmt.Sprintf("binary %s operation", n.Op)
- case *ast.BlockStmt:
- return "block"
- case *ast.BranchStmt:
- switch n.Tok {
- case token.BREAK:
- return "break statement"
- case token.CONTINUE:
- return "continue statement"
- case token.GOTO:
- return "goto statement"
- case token.FALLTHROUGH:
- return "fall-through statement"
- }
- case *ast.CallExpr:
- if len(n.Args) == 1 && !n.Ellipsis.IsValid() {
- return "function call (or conversion)"
- }
- return "function call"
- case *ast.CaseClause:
- return "case clause"
- case *ast.ChanType:
- return "channel type"
- case *ast.CommClause:
- return "communication clause"
- case *ast.Comment:
- return "comment"
- case *ast.CommentGroup:
- return "comment group"
- case *ast.CompositeLit:
- return "composite literal"
- case *ast.DeclStmt:
- return NodeDescription(n.Decl) + " statement"
- case *ast.DeferStmt:
- return "defer statement"
- case *ast.Ellipsis:
- return "ellipsis"
- case *ast.EmptyStmt:
- return "empty statement"
- case *ast.ExprStmt:
- return "expression statement"
- case *ast.Field:
- // Can be any of these:
- // struct {x, y int} -- struct field(s)
- // struct {T} -- anon struct field
- // interface {I} -- interface embedding
- // interface {f()} -- interface method
- // func (A) func(B) C -- receiver, param(s), result(s)
- return "field/method/parameter"
- case *ast.FieldList:
- return "field/method/parameter list"
- case *ast.File:
- return "source file"
- case *ast.ForStmt:
- return "for loop"
- case *ast.FuncDecl:
- return "function declaration"
- case *ast.FuncLit:
- return "function literal"
- case *ast.FuncType:
- return "function type"
- case *ast.GenDecl:
- switch n.Tok {
- case token.IMPORT:
- return "import declaration"
- case token.CONST:
- return "constant declaration"
- case token.TYPE:
- return "type declaration"
- case token.VAR:
- return "variable declaration"
- }
- case *ast.GoStmt:
- return "go statement"
- case *ast.Ident:
- return "identifier"
- case *ast.IfStmt:
- return "if statement"
- case *ast.ImportSpec:
- return "import specification"
- case *ast.IncDecStmt:
- if n.Tok == token.INC {
- return "increment statement"
- }
- return "decrement statement"
- case *ast.IndexExpr:
- return "index expression"
- case *ast.IndexListExpr:
- return "index list expression"
- case *ast.InterfaceType:
- return "interface type"
- case *ast.KeyValueExpr:
- return "key/value association"
- case *ast.LabeledStmt:
- return "statement label"
- case *ast.MapType:
- return "map type"
- case *ast.Package:
- return "package"
- case *ast.ParenExpr:
- return "parenthesized " + NodeDescription(n.X)
- case *ast.RangeStmt:
- return "range loop"
- case *ast.ReturnStmt:
- return "return statement"
- case *ast.SelectStmt:
- return "select statement"
- case *ast.SelectorExpr:
- return "selector"
- case *ast.SendStmt:
- return "channel send"
- case *ast.SliceExpr:
- return "slice expression"
- case *ast.StarExpr:
- return "*-operation" // load/store expr or pointer type
- case *ast.StructType:
- return "struct type"
- case *ast.SwitchStmt:
- return "switch statement"
- case *ast.TypeAssertExpr:
- return "type assertion"
- case *ast.TypeSpec:
- return "type specification"
- case *ast.TypeSwitchStmt:
- return "type switch"
- case *ast.UnaryExpr:
- return fmt.Sprintf("unary %s operation", n.Op)
- case *ast.ValueSpec:
- return "value specification"
-
- }
- panic(fmt.Sprintf("unexpected node type: %T", n))
-}
-
-func is[T any](x any) bool {
- _, ok := x.(T)
- return ok
-}
diff --git a/vendor/golang.org/x/tools/go/ast/astutil/imports.go b/vendor/golang.org/x/tools/go/ast/astutil/imports.go
deleted file mode 100644
index a6b5ed0a8..000000000
--- a/vendor/golang.org/x/tools/go/ast/astutil/imports.go
+++ /dev/null
@@ -1,490 +0,0 @@
-// Copyright 2013 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.
-
-// Package astutil contains common utilities for working with the Go AST.
-package astutil // import "golang.org/x/tools/go/ast/astutil"
-
-import (
- "fmt"
- "go/ast"
- "go/token"
- "strconv"
- "strings"
-)
-
-// AddImport adds the import path to the file f, if absent.
-func AddImport(fset *token.FileSet, f *ast.File, path string) (added bool) {
- return AddNamedImport(fset, f, "", path)
-}
-
-// AddNamedImport adds the import with the given name and path to the file f, if absent.
-// If name is not empty, it is used to rename the import.
-//
-// For example, calling
-//
-// AddNamedImport(fset, f, "pathpkg", "path")
-//
-// adds
-//
-// import pathpkg "path"
-func AddNamedImport(fset *token.FileSet, f *ast.File, name, path string) (added bool) {
- if imports(f, name, path) {
- return false
- }
-
- newImport := &ast.ImportSpec{
- Path: &ast.BasicLit{
- Kind: token.STRING,
- Value: strconv.Quote(path),
- },
- }
- if name != "" {
- newImport.Name = &ast.Ident{Name: name}
- }
-
- // Find an import decl to add to.
- // The goal is to find an existing import
- // whose import path has the longest shared
- // prefix with path.
- var (
- bestMatch = -1 // length of longest shared prefix
- lastImport = -1 // index in f.Decls of the file's final import decl
- impDecl *ast.GenDecl // import decl containing the best match
- impIndex = -1 // spec index in impDecl containing the best match
-
- isThirdPartyPath = isThirdParty(path)
- )
- for i, decl := range f.Decls {
- gen, ok := decl.(*ast.GenDecl)
- if ok && gen.Tok == token.IMPORT {
- lastImport = i
- // Do not add to import "C", to avoid disrupting the
- // association with its doc comment, breaking cgo.
- if declImports(gen, "C") {
- continue
- }
-
- // Match an empty import decl if that's all that is available.
- if len(gen.Specs) == 0 && bestMatch == -1 {
- impDecl = gen
- }
-
- // Compute longest shared prefix with imports in this group and find best
- // matched import spec.
- // 1. Always prefer import spec with longest shared prefix.
- // 2. While match length is 0,
- // - for stdlib package: prefer first import spec.
- // - for third party package: prefer first third party import spec.
- // We cannot use last import spec as best match for third party package
- // because grouped imports are usually placed last by goimports -local
- // flag.
- // See issue #19190.
- seenAnyThirdParty := false
- for j, spec := range gen.Specs {
- impspec := spec.(*ast.ImportSpec)
- p := importPath(impspec)
- n := matchLen(p, path)
- if n > bestMatch || (bestMatch == 0 && !seenAnyThirdParty && isThirdPartyPath) {
- bestMatch = n
- impDecl = gen
- impIndex = j
- }
- seenAnyThirdParty = seenAnyThirdParty || isThirdParty(p)
- }
- }
- }
-
- // If no import decl found, add one after the last import.
- if impDecl == nil {
- impDecl = &ast.GenDecl{
- Tok: token.IMPORT,
- }
- if lastImport >= 0 {
- impDecl.TokPos = f.Decls[lastImport].End()
- } else {
- // There are no existing imports.
- // Our new import, preceded by a blank line, goes after the package declaration
- // and after the comment, if any, that starts on the same line as the
- // package declaration.
- impDecl.TokPos = f.Package
-
- file := fset.File(f.Package)
- pkgLine := file.Line(f.Package)
- for _, c := range f.Comments {
- if file.Line(c.Pos()) > pkgLine {
- break
- }
- // +2 for a blank line
- impDecl.TokPos = c.End() + 2
- }
- }
- f.Decls = append(f.Decls, nil)
- copy(f.Decls[lastImport+2:], f.Decls[lastImport+1:])
- f.Decls[lastImport+1] = impDecl
- }
-
- // Insert new import at insertAt.
- insertAt := 0
- if impIndex >= 0 {
- // insert after the found import
- insertAt = impIndex + 1
- }
- impDecl.Specs = append(impDecl.Specs, nil)
- copy(impDecl.Specs[insertAt+1:], impDecl.Specs[insertAt:])
- impDecl.Specs[insertAt] = newImport
- pos := impDecl.Pos()
- if insertAt > 0 {
- // If there is a comment after an existing import, preserve the comment
- // position by adding the new import after the comment.
- if spec, ok := impDecl.Specs[insertAt-1].(*ast.ImportSpec); ok && spec.Comment != nil {
- pos = spec.Comment.End()
- } else {
- // Assign same position as the previous import,
- // so that the sorter sees it as being in the same block.
- pos = impDecl.Specs[insertAt-1].Pos()
- }
- }
- if newImport.Name != nil {
- newImport.Name.NamePos = pos
- }
- newImport.Path.ValuePos = pos
- newImport.EndPos = pos
-
- // Clean up parens. impDecl contains at least one spec.
- if len(impDecl.Specs) == 1 {
- // Remove unneeded parens.
- impDecl.Lparen = token.NoPos
- } else if !impDecl.Lparen.IsValid() {
- // impDecl needs parens added.
- impDecl.Lparen = impDecl.Specs[0].Pos()
- }
-
- f.Imports = append(f.Imports, newImport)
-
- if len(f.Decls) <= 1 {
- return true
- }
-
- // Merge all the import declarations into the first one.
- var first *ast.GenDecl
- for i := 0; i < len(f.Decls); i++ {
- decl := f.Decls[i]
- gen, ok := decl.(*ast.GenDecl)
- if !ok || gen.Tok != token.IMPORT || declImports(gen, "C") {
- continue
- }
- if first == nil {
- first = gen
- continue // Don't touch the first one.
- }
- // We now know there is more than one package in this import
- // declaration. Ensure that it ends up parenthesized.
- first.Lparen = first.Pos()
- // Move the imports of the other import declaration to the first one.
- for _, spec := range gen.Specs {
- spec.(*ast.ImportSpec).Path.ValuePos = first.Pos()
- first.Specs = append(first.Specs, spec)
- }
- f.Decls = append(f.Decls[:i], f.Decls[i+1:]...)
- i--
- }
-
- return true
-}
-
-func isThirdParty(importPath string) bool {
- // Third party package import path usually contains "." (".com", ".org", ...)
- // This logic is taken from golang.org/x/tools/imports package.
- return strings.Contains(importPath, ".")
-}
-
-// DeleteImport deletes the import path from the file f, if present.
-// If there are duplicate import declarations, all matching ones are deleted.
-func DeleteImport(fset *token.FileSet, f *ast.File, path string) (deleted bool) {
- return DeleteNamedImport(fset, f, "", path)
-}
-
-// DeleteNamedImport deletes the import with the given name and path from the file f, if present.
-// If there are duplicate import declarations, all matching ones are deleted.
-func DeleteNamedImport(fset *token.FileSet, f *ast.File, name, path string) (deleted bool) {
- var delspecs []*ast.ImportSpec
- var delcomments []*ast.CommentGroup
-
- // Find the import nodes that import path, if any.
- for i := 0; i < len(f.Decls); i++ {
- decl := f.Decls[i]
- gen, ok := decl.(*ast.GenDecl)
- if !ok || gen.Tok != token.IMPORT {
- continue
- }
- for j := 0; j < len(gen.Specs); j++ {
- spec := gen.Specs[j]
- impspec := spec.(*ast.ImportSpec)
- if importName(impspec) != name || importPath(impspec) != path {
- continue
- }
-
- // We found an import spec that imports path.
- // Delete it.
- delspecs = append(delspecs, impspec)
- deleted = true
- copy(gen.Specs[j:], gen.Specs[j+1:])
- gen.Specs = gen.Specs[:len(gen.Specs)-1]
-
- // If this was the last import spec in this decl,
- // delete the decl, too.
- if len(gen.Specs) == 0 {
- copy(f.Decls[i:], f.Decls[i+1:])
- f.Decls = f.Decls[:len(f.Decls)-1]
- i--
- break
- } else if len(gen.Specs) == 1 {
- if impspec.Doc != nil {
- delcomments = append(delcomments, impspec.Doc)
- }
- if impspec.Comment != nil {
- delcomments = append(delcomments, impspec.Comment)
- }
- for _, cg := range f.Comments {
- // Found comment on the same line as the import spec.
- if cg.End() < impspec.Pos() && fset.Position(cg.End()).Line == fset.Position(impspec.Pos()).Line {
- delcomments = append(delcomments, cg)
- break
- }
- }
-
- spec := gen.Specs[0].(*ast.ImportSpec)
-
- // Move the documentation right after the import decl.
- if spec.Doc != nil {
- for fset.Position(gen.TokPos).Line+1 < fset.Position(spec.Doc.Pos()).Line {
- fset.File(gen.TokPos).MergeLine(fset.Position(gen.TokPos).Line)
- }
- }
- for _, cg := range f.Comments {
- if cg.End() < spec.Pos() && fset.Position(cg.End()).Line == fset.Position(spec.Pos()).Line {
- for fset.Position(gen.TokPos).Line+1 < fset.Position(spec.Pos()).Line {
- fset.File(gen.TokPos).MergeLine(fset.Position(gen.TokPos).Line)
- }
- break
- }
- }
- }
- if j > 0 {
- lastImpspec := gen.Specs[j-1].(*ast.ImportSpec)
- lastLine := fset.PositionFor(lastImpspec.Path.ValuePos, false).Line
- line := fset.PositionFor(impspec.Path.ValuePos, false).Line
-
- // We deleted an entry but now there may be
- // a blank line-sized hole where the import was.
- if line-lastLine > 1 || !gen.Rparen.IsValid() {
- // There was a blank line immediately preceding the deleted import,
- // so there's no need to close the hole. The right parenthesis is
- // invalid after AddImport to an import statement without parenthesis.
- // Do nothing.
- } else if line != fset.File(gen.Rparen).LineCount() {
- // There was no blank line. Close the hole.
- fset.File(gen.Rparen).MergeLine(line)
- }
- }
- j--
- }
- }
-
- // Delete imports from f.Imports.
- for i := 0; i < len(f.Imports); i++ {
- imp := f.Imports[i]
- for j, del := range delspecs {
- if imp == del {
- copy(f.Imports[i:], f.Imports[i+1:])
- f.Imports = f.Imports[:len(f.Imports)-1]
- copy(delspecs[j:], delspecs[j+1:])
- delspecs = delspecs[:len(delspecs)-1]
- i--
- break
- }
- }
- }
-
- // Delete comments from f.Comments.
- for i := 0; i < len(f.Comments); i++ {
- cg := f.Comments[i]
- for j, del := range delcomments {
- if cg == del {
- copy(f.Comments[i:], f.Comments[i+1:])
- f.Comments = f.Comments[:len(f.Comments)-1]
- copy(delcomments[j:], delcomments[j+1:])
- delcomments = delcomments[:len(delcomments)-1]
- i--
- break
- }
- }
- }
-
- if len(delspecs) > 0 {
- panic(fmt.Sprintf("deleted specs from Decls but not Imports: %v", delspecs))
- }
-
- return
-}
-
-// RewriteImport rewrites any import of path oldPath to path newPath.
-func RewriteImport(fset *token.FileSet, f *ast.File, oldPath, newPath string) (rewrote bool) {
- for _, imp := range f.Imports {
- if importPath(imp) == oldPath {
- rewrote = true
- // record old End, because the default is to compute
- // it using the length of imp.Path.Value.
- imp.EndPos = imp.End()
- imp.Path.Value = strconv.Quote(newPath)
- }
- }
- return
-}
-
-// UsesImport reports whether a given import is used.
-// The provided File must have been parsed with syntactic object resolution
-// (not using go/parser.SkipObjectResolution).
-func UsesImport(f *ast.File, path string) (used bool) {
- if f.Scope == nil {
- panic("file f was not parsed with syntactic object resolution")
- }
- spec := importSpec(f, path)
- if spec == nil {
- return
- }
-
- name := spec.Name.String()
- switch name {
- case "<nil>":
- // If the package name is not explicitly specified,
- // make an educated guess. This is not guaranteed to be correct.
- lastSlash := strings.LastIndex(path, "/")
- if lastSlash == -1 {
- name = path
- } else {
- name = path[lastSlash+1:]
- }
- case "_", ".":
- // Not sure if this import is used - err on the side of caution.
- return true
- }
-
- ast.Walk(visitFn(func(n ast.Node) {
- sel, ok := n.(*ast.SelectorExpr)
- if ok && isTopName(sel.X, name) {
- used = true
- }
- }), f)
-
- return
-}
-
-type visitFn func(node ast.Node)
-
-func (fn visitFn) Visit(node ast.Node) ast.Visitor {
- fn(node)
- return fn
-}
-
-// imports reports whether f has an import with the specified name and path.
-func imports(f *ast.File, name, path string) bool {
- for _, s := range f.Imports {
- if importName(s) == name && importPath(s) == path {
- return true
- }
- }
- return false
-}
-
-// importSpec returns the import spec if f imports path,
-// or nil otherwise.
-func importSpec(f *ast.File, path string) *ast.ImportSpec {
- for _, s := range f.Imports {
- if importPath(s) == path {
- return s
- }
- }
- return nil
-}
-
-// importName returns the name of s,
-// or "" if the import is not named.
-func importName(s *ast.ImportSpec) string {
- if s.Name == nil {
- return ""
- }
- return s.Name.Name
-}
-
-// importPath returns the unquoted import path of s,
-// or "" if the path is not properly quoted.
-func importPath(s *ast.ImportSpec) string {
- t, err := strconv.Unquote(s.Path.Value)
- if err != nil {
- return ""
- }
- return t
-}
-
-// declImports reports whether gen contains an import of path.
-func declImports(gen *ast.GenDecl, path string) bool {
- if gen.Tok != token.IMPORT {
- return false
- }
- for _, spec := range gen.Specs {
- impspec := spec.(*ast.ImportSpec)
- if importPath(impspec) == path {
- return true
- }
- }
- return false
-}
-
-// matchLen returns the length of the longest path segment prefix shared by x and y.
-func matchLen(x, y string) int {
- n := 0
- for i := 0; i < len(x) && i < len(y) && x[i] == y[i]; i++ {
- if x[i] == '/' {
- n++
- }
- }
- return n
-}
-
-// isTopName returns true if n is a top-level unresolved identifier with the given name.
-func isTopName(n ast.Expr, name string) bool {
- id, ok := n.(*ast.Ident)
- return ok && id.Name == name && id.Obj == nil
-}
-
-// Imports returns the file imports grouped by paragraph.
-func Imports(fset *token.FileSet, f *ast.File) [][]*ast.ImportSpec {
- var groups [][]*ast.ImportSpec
-
- for _, decl := range f.Decls {
- genDecl, ok := decl.(*ast.GenDecl)
- if !ok || genDecl.Tok != token.IMPORT {
- break
- }
-
- group := []*ast.ImportSpec{}
-
- var lastLine int
- for _, spec := range genDecl.Specs {
- importSpec := spec.(*ast.ImportSpec)
- pos := importSpec.Path.ValuePos
- line := fset.Position(pos).Line
- if lastLine > 0 && pos > 0 && line-lastLine > 1 {
- groups = append(groups, group)
- group = []*ast.ImportSpec{}
- }
- group = append(group, importSpec)
- lastLine = line
- }
- groups = append(groups, group)
- }
-
- return groups
-}
diff --git a/vendor/golang.org/x/tools/go/ast/astutil/rewrite.go b/vendor/golang.org/x/tools/go/ast/astutil/rewrite.go
deleted file mode 100644
index 58934f766..000000000
--- a/vendor/golang.org/x/tools/go/ast/astutil/rewrite.go
+++ /dev/null
@@ -1,486 +0,0 @@
-// Copyright 2017 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.
-
-package astutil
-
-import (
- "fmt"
- "go/ast"
- "reflect"
- "sort"
-)
-
-// An ApplyFunc is invoked by Apply for each node n, even if n is nil,
-// before and/or after the node's children, using a Cursor describing
-// the current node and providing operations on it.
-//
-// The return value of ApplyFunc controls the syntax tree traversal.
-// See Apply for details.
-type ApplyFunc func(*Cursor) bool
-
-// Apply traverses a syntax tree recursively, starting with root,
-// and calling pre and post for each node as described below.
-// Apply returns the syntax tree, possibly modified.
-//
-// If pre is not nil, it is called for each node before the node's
-// children are traversed (pre-order). If pre returns false, no
-// children are traversed, and post is not called for that node.
-//
-// If post is not nil, and a prior call of pre didn't return false,
-// post is called for each node after its children are traversed
-// (post-order). If post returns false, traversal is terminated and
-// Apply returns immediately.
-//
-// Only fields that refer to AST nodes are considered children;
-// i.e., token.Pos, Scopes, Objects, and fields of basic types
-// (strings, etc.) are ignored.
-//
-// Children are traversed in the order in which they appear in the
-// respective node's struct definition. A package's files are
-// traversed in the filenames' alphabetical order.
-func Apply(root ast.Node, pre, post ApplyFunc) (result ast.Node) {
- parent := &struct{ ast.Node }{root}
- defer func() {
- if r := recover(); r != nil && r != abort {
- panic(r)
- }
- result = parent.Node
- }()
- a := &application{pre: pre, post: post}
- a.apply(parent, "Node", nil, root)
- return
-}
-
-var abort = new(int) // singleton, to signal termination of Apply
-
-// A Cursor describes a node encountered during Apply.
-// Information about the node and its parent is available
-// from the Node, Parent, Name, and Index methods.
-//
-// If p is a variable of type and value of the current parent node
-// c.Parent(), and f is the field identifier with name c.Name(),
-// the following invariants hold:
-//
-// p.f == c.Node() if c.Index() < 0
-// p.f[c.Index()] == c.Node() if c.Index() >= 0
-//
-// The methods Replace, Delete, InsertBefore, and InsertAfter
-// can be used to change the AST without disrupting Apply.
-type Cursor struct {
- parent ast.Node
- name string
- iter *iterator // valid if non-nil
- node ast.Node
-}
-
-// Node returns the current Node.
-func (c *Cursor) Node() ast.Node { return c.node }
-
-// Parent returns the parent of the current Node.
-func (c *Cursor) Parent() ast.Node { return c.parent }
-
-// Name returns the name of the parent Node field that contains the current Node.
-// If the parent is a *ast.Package and the current Node is a *ast.File, Name returns
-// the filename for the current Node.
-func (c *Cursor) Name() string { return c.name }
-
-// Index reports the index >= 0 of the current Node in the slice of Nodes that
-// contains it, or a value < 0 if the current Node is not part of a slice.
-// The index of the current node changes if InsertBefore is called while
-// processing the current node.
-func (c *Cursor) Index() int {
- if c.iter != nil {
- return c.iter.index
- }
- return -1
-}
-
-// field returns the current node's parent field value.
-func (c *Cursor) field() reflect.Value {
- return reflect.Indirect(reflect.ValueOf(c.parent)).FieldByName(c.name)
-}
-
-// Replace replaces the current Node with n.
-// The replacement node is not walked by Apply.
-func (c *Cursor) Replace(n ast.Node) {
- if _, ok := c.node.(*ast.File); ok {
- file, ok := n.(*ast.File)
- if !ok {
- panic("attempt to replace *ast.File with non-*ast.File")
- }
- c.parent.(*ast.Package).Files[c.name] = file
- return
- }
-
- v := c.field()
- if i := c.Index(); i >= 0 {
- v = v.Index(i)
- }
- v.Set(reflect.ValueOf(n))
-}
-
-// Delete deletes the current Node from its containing slice.
-// If the current Node is not part of a slice, Delete panics.
-// As a special case, if the current node is a package file,
-// Delete removes it from the package's Files map.
-func (c *Cursor) Delete() {
- if _, ok := c.node.(*ast.File); ok {
- delete(c.parent.(*ast.Package).Files, c.name)
- return
- }
-
- i := c.Index()
- if i < 0 {
- panic("Delete node not contained in slice")
- }
- v := c.field()
- l := v.Len()
- reflect.Copy(v.Slice(i, l), v.Slice(i+1, l))
- v.Index(l - 1).Set(reflect.Zero(v.Type().Elem()))
- v.SetLen(l - 1)
- c.iter.step--
-}
-
-// InsertAfter inserts n after the current Node in its containing slice.
-// If the current Node is not part of a slice, InsertAfter panics.
-// Apply does not walk n.
-func (c *Cursor) InsertAfter(n ast.Node) {
- i := c.Index()
- if i < 0 {
- panic("InsertAfter node not contained in slice")
- }
- v := c.field()
- v.Set(reflect.Append(v, reflect.Zero(v.Type().Elem())))
- l := v.Len()
- reflect.Copy(v.Slice(i+2, l), v.Slice(i+1, l))
- v.Index(i + 1).Set(reflect.ValueOf(n))
- c.iter.step++
-}
-
-// InsertBefore inserts n before the current Node in its containing slice.
-// If the current Node is not part of a slice, InsertBefore panics.
-// Apply will not walk n.
-func (c *Cursor) InsertBefore(n ast.Node) {
- i := c.Index()
- if i < 0 {
- panic("InsertBefore node not contained in slice")
- }
- v := c.field()
- v.Set(reflect.Append(v, reflect.Zero(v.Type().Elem())))
- l := v.Len()
- reflect.Copy(v.Slice(i+1, l), v.Slice(i, l))
- v.Index(i).Set(reflect.ValueOf(n))
- c.iter.index++
-}
-
-// application carries all the shared data so we can pass it around cheaply.
-type application struct {
- pre, post ApplyFunc
- cursor Cursor
- iter iterator
-}
-
-func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.Node) {
- // convert typed nil into untyped nil
- if v := reflect.ValueOf(n); v.Kind() == reflect.Ptr && v.IsNil() {
- n = nil
- }
-
- // avoid heap-allocating a new cursor for each apply call; reuse a.cursor instead
- saved := a.cursor
- a.cursor.parent = parent
- a.cursor.name = name
- a.cursor.iter = iter
- a.cursor.node = n
-
- if a.pre != nil && !a.pre(&a.cursor) {
- a.cursor = saved
- return
- }
-
- // walk children
- // (the order of the cases matches the order of the corresponding node types in go/ast)
- switch n := n.(type) {
- case nil:
- // nothing to do
-
- // Comments and fields
- case *ast.Comment:
- // nothing to do
-
- case *ast.CommentGroup:
- if n != nil {
- a.applyList(n, "List")
- }
-
- case *ast.Field:
- a.apply(n, "Doc", nil, n.Doc)
- a.applyList(n, "Names")
- a.apply(n, "Type", nil, n.Type)
- a.apply(n, "Tag", nil, n.Tag)
- a.apply(n, "Comment", nil, n.Comment)
-
- case *ast.FieldList:
- a.applyList(n, "List")
-
- // Expressions
- case *ast.BadExpr, *ast.Ident, *ast.BasicLit:
- // nothing to do
-
- case *ast.Ellipsis:
- a.apply(n, "Elt", nil, n.Elt)
-
- case *ast.FuncLit:
- a.apply(n, "Type", nil, n.Type)
- a.apply(n, "Body", nil, n.Body)
-
- case *ast.CompositeLit:
- a.apply(n, "Type", nil, n.Type)
- a.applyList(n, "Elts")
-
- case *ast.ParenExpr:
- a.apply(n, "X", nil, n.X)
-
- case *ast.SelectorExpr:
- a.apply(n, "X", nil, n.X)
- a.apply(n, "Sel", nil, n.Sel)
-
- case *ast.IndexExpr:
- a.apply(n, "X", nil, n.X)
- a.apply(n, "Index", nil, n.Index)
-
- case *ast.IndexListExpr:
- a.apply(n, "X", nil, n.X)
- a.applyList(n, "Indices")
-
- case *ast.SliceExpr:
- a.apply(n, "X", nil, n.X)
- a.apply(n, "Low", nil, n.Low)
- a.apply(n, "High", nil, n.High)
- a.apply(n, "Max", nil, n.Max)
-
- case *ast.TypeAssertExpr:
- a.apply(n, "X", nil, n.X)
- a.apply(n, "Type", nil, n.Type)
-
- case *ast.CallExpr:
- a.apply(n, "Fun", nil, n.Fun)
- a.applyList(n, "Args")
-
- case *ast.StarExpr:
- a.apply(n, "X", nil, n.X)
-
- case *ast.UnaryExpr:
- a.apply(n, "X", nil, n.X)
-
- case *ast.BinaryExpr:
- a.apply(n, "X", nil, n.X)
- a.apply(n, "Y", nil, n.Y)
-
- case *ast.KeyValueExpr:
- a.apply(n, "Key", nil, n.Key)
- a.apply(n, "Value", nil, n.Value)
-
- // Types
- case *ast.ArrayType:
- a.apply(n, "Len", nil, n.Len)
- a.apply(n, "Elt", nil, n.Elt)
-
- case *ast.StructType:
- a.apply(n, "Fields", nil, n.Fields)
-
- case *ast.FuncType:
- if tparams := n.TypeParams; tparams != nil {
- a.apply(n, "TypeParams", nil, tparams)
- }
- a.apply(n, "Params", nil, n.Params)
- a.apply(n, "Results", nil, n.Results)
-
- case *ast.InterfaceType:
- a.apply(n, "Methods", nil, n.Methods)
-
- case *ast.MapType:
- a.apply(n, "Key", nil, n.Key)
- a.apply(n, "Value", nil, n.Value)
-
- case *ast.ChanType:
- a.apply(n, "Value", nil, n.Value)
-
- // Statements
- case *ast.BadStmt:
- // nothing to do
-
- case *ast.DeclStmt:
- a.apply(n, "Decl", nil, n.Decl)
-
- case *ast.EmptyStmt:
- // nothing to do
-
- case *ast.LabeledStmt:
- a.apply(n, "Label", nil, n.Label)
- a.apply(n, "Stmt", nil, n.Stmt)
-
- case *ast.ExprStmt:
- a.apply(n, "X", nil, n.X)
-
- case *ast.SendStmt:
- a.apply(n, "Chan", nil, n.Chan)
- a.apply(n, "Value", nil, n.Value)
-
- case *ast.IncDecStmt:
- a.apply(n, "X", nil, n.X)
-
- case *ast.AssignStmt:
- a.applyList(n, "Lhs")
- a.applyList(n, "Rhs")
-
- case *ast.GoStmt:
- a.apply(n, "Call", nil, n.Call)
-
- case *ast.DeferStmt:
- a.apply(n, "Call", nil, n.Call)
-
- case *ast.ReturnStmt:
- a.applyList(n, "Results")
-
- case *ast.BranchStmt:
- a.apply(n, "Label", nil, n.Label)
-
- case *ast.BlockStmt:
- a.applyList(n, "List")
-
- case *ast.IfStmt:
- a.apply(n, "Init", nil, n.Init)
- a.apply(n, "Cond", nil, n.Cond)
- a.apply(n, "Body", nil, n.Body)
- a.apply(n, "Else", nil, n.Else)
-
- case *ast.CaseClause:
- a.applyList(n, "List")
- a.applyList(n, "Body")
-
- case *ast.SwitchStmt:
- a.apply(n, "Init", nil, n.Init)
- a.apply(n, "Tag", nil, n.Tag)
- a.apply(n, "Body", nil, n.Body)
-
- case *ast.TypeSwitchStmt:
- a.apply(n, "Init", nil, n.Init)
- a.apply(n, "Assign", nil, n.Assign)
- a.apply(n, "Body", nil, n.Body)
-
- case *ast.CommClause:
- a.apply(n, "Comm", nil, n.Comm)
- a.applyList(n, "Body")
-
- case *ast.SelectStmt:
- a.apply(n, "Body", nil, n.Body)
-
- case *ast.ForStmt:
- a.apply(n, "Init", nil, n.Init)
- a.apply(n, "Cond", nil, n.Cond)
- a.apply(n, "Post", nil, n.Post)
- a.apply(n, "Body", nil, n.Body)
-
- case *ast.RangeStmt:
- a.apply(n, "Key", nil, n.Key)
- a.apply(n, "Value", nil, n.Value)
- a.apply(n, "X", nil, n.X)
- a.apply(n, "Body", nil, n.Body)
-
- // Declarations
- case *ast.ImportSpec:
- a.apply(n, "Doc", nil, n.Doc)
- a.apply(n, "Name", nil, n.Name)
- a.apply(n, "Path", nil, n.Path)
- a.apply(n, "Comment", nil, n.Comment)
-
- case *ast.ValueSpec:
- a.apply(n, "Doc", nil, n.Doc)
- a.applyList(n, "Names")
- a.apply(n, "Type", nil, n.Type)
- a.applyList(n, "Values")
- a.apply(n, "Comment", nil, n.Comment)
-
- case *ast.TypeSpec:
- a.apply(n, "Doc", nil, n.Doc)
- a.apply(n, "Name", nil, n.Name)
- if tparams := n.TypeParams; tparams != nil {
- a.apply(n, "TypeParams", nil, tparams)
- }
- a.apply(n, "Type", nil, n.Type)
- a.apply(n, "Comment", nil, n.Comment)
-
- case *ast.BadDecl:
- // nothing to do
-
- case *ast.GenDecl:
- a.apply(n, "Doc", nil, n.Doc)
- a.applyList(n, "Specs")
-
- case *ast.FuncDecl:
- a.apply(n, "Doc", nil, n.Doc)
- a.apply(n, "Recv", nil, n.Recv)
- a.apply(n, "Name", nil, n.Name)
- a.apply(n, "Type", nil, n.Type)
- a.apply(n, "Body", nil, n.Body)
-
- // Files and packages
- case *ast.File:
- a.apply(n, "Doc", nil, n.Doc)
- a.apply(n, "Name", nil, n.Name)
- a.applyList(n, "Decls")
- // Don't walk n.Comments; they have either been walked already if
- // they are Doc comments, or they can be easily walked explicitly.
-
- case *ast.Package:
- // collect and sort names for reproducible behavior
- var names []string
- for name := range n.Files {
- names = append(names, name)
- }
- sort.Strings(names)
- for _, name := range names {
- a.apply(n, name, nil, n.Files[name])
- }
-
- default:
- panic(fmt.Sprintf("Apply: unexpected node type %T", n))
- }
-
- if a.post != nil && !a.post(&a.cursor) {
- panic(abort)
- }
-
- a.cursor = saved
-}
-
-// An iterator controls iteration over a slice of nodes.
-type iterator struct {
- index, step int
-}
-
-func (a *application) applyList(parent ast.Node, name string) {
- // avoid heap-allocating a new iterator for each applyList call; reuse a.iter instead
- saved := a.iter
- a.iter.index = 0
- for {
- // must reload parent.name each time, since cursor modifications might change it
- v := reflect.Indirect(reflect.ValueOf(parent)).FieldByName(name)
- if a.iter.index >= v.Len() {
- break
- }
-
- // element x may be nil in a bad AST - be cautious
- var x ast.Node
- if e := v.Index(a.iter.index); e.IsValid() {
- x = e.Interface().(ast.Node)
- }
-
- a.iter.step = 1
- a.apply(parent, name, &a.iter, x)
- a.iter.index += a.iter.step
- }
- a.iter = saved
-}
diff --git a/vendor/golang.org/x/tools/go/ast/astutil/util.go b/vendor/golang.org/x/tools/go/ast/astutil/util.go
deleted file mode 100644
index ca71e3e10..000000000
--- a/vendor/golang.org/x/tools/go/ast/astutil/util.go
+++ /dev/null
@@ -1,11 +0,0 @@
-// Copyright 2015 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.
-
-package astutil
-
-import "go/ast"
-
-// Unparen returns e with any enclosing parentheses stripped.
-// Deprecated: use [ast.Unparen].
-func Unparen(e ast.Expr) ast.Expr { return ast.Unparen(e) }
diff --git a/vendor/golang.org/x/tools/go/gcexportdata/gcexportdata.go b/vendor/golang.org/x/tools/go/gcexportdata/gcexportdata.go
deleted file mode 100644
index 65fe2628e..000000000
--- a/vendor/golang.org/x/tools/go/gcexportdata/gcexportdata.go
+++ /dev/null
@@ -1,239 +0,0 @@
-// Copyright 2016 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.
-
-// Package gcexportdata provides functions for reading and writing
-// export data, which is a serialized description of the API of a Go
-// package including the names, kinds, types, and locations of all
-// exported declarations.
-//
-// The standard Go compiler (cmd/compile) writes an export data file
-// for each package it compiles, which it later reads when compiling
-// packages that import the earlier one. The compiler must thus
-// contain logic to both write and read export data.
-// (See the "Export" section in the cmd/compile/README file.)
-//
-// The [Read] function in this package can read files produced by the
-// compiler, producing [go/types] data structures. As a matter of
-// policy, Read supports export data files produced by only the last
-// two Go releases plus tip; see https://go.dev/issue/68898. The
-// export data files produced by the compiler contain additional
-// details related to generics, inlining, and other optimizations that
-// cannot be decoded by the [Read] function.
-//
-// In files written by the compiler, the export data is not at the
-// start of the file. Before calling Read, use [NewReader] to locate
-// the desired portion of the file.
-//
-// The [Write] function in this package encodes the exported API of a
-// Go package ([types.Package]) as a file. Such files can be later
-// decoded by Read, but cannot be consumed by the compiler.
-//
-// # Future changes
-//
-// Although Read supports the formats written by both Write and the
-// compiler, the two are quite different, and there is an open
-// proposal (https://go.dev/issue/69491) to separate these APIs.
-//
-// Under that proposal, this package would ultimately provide only the
-// Read operation for compiler export data, which must be defined in
-// this module (golang.org/x/tools), not in the standard library, to
-// avoid version skew for developer tools that need to read compiler
-// export data both before and after a Go release, such as from Go
-// 1.23 to Go 1.24. Because this package lives in the tools module,
-// clients can update their version of the module some time before the
-// Go 1.24 release and rebuild and redeploy their tools, which will
-// then be able to consume both Go 1.23 and Go 1.24 export data files,
-// so they will work before and after the Go update. (See discussion
-// at https://go.dev/issue/15651.)
-//
-// The operations to import and export [go/types] data structures
-// would be defined in the go/types package as Import and Export.
-// [Write] would (eventually) delegate to Export,
-// and [Read], when it detects a file produced by Export,
-// would delegate to Import.
-//
-// # Deprecations
-//
-// The [NewImporter] and [Find] functions are deprecated and should
-// not be used in new code. The [WriteBundle] and [ReadBundle]
-// functions are experimental, and there is an open proposal to
-// deprecate them (https://go.dev/issue/69573).
-package gcexportdata
-
-import (
- "bufio"
- "bytes"
- "encoding/json"
- "fmt"
- "go/token"
- "go/types"
- "io"
- "os/exec"
-
- "golang.org/x/tools/internal/gcimporter"
-)
-
-// Find returns the name of an object (.o) or archive (.a) file
-// containing type information for the specified import path,
-// using the go command.
-// If no file was found, an empty filename is returned.
-//
-// A relative srcDir is interpreted relative to the current working directory.
-//
-// Find also returns the package's resolved (canonical) import path,
-// reflecting the effects of srcDir and vendoring on importPath.
-//
-// Deprecated: Use the higher-level API in golang.org/x/tools/go/packages,
-// which is more efficient.
-func Find(importPath, srcDir string) (filename, path string) {
- cmd := exec.Command("go", "list", "-json", "-export", "--", importPath)
- cmd.Dir = srcDir
- out, err := cmd.Output()
- if err != nil {
- return "", ""
- }
- var data struct {
- ImportPath string
- Export string
- }
- json.Unmarshal(out, &data)
- return data.Export, data.ImportPath
-}
-
-// NewReader returns a reader for the export data section of an object
-// (.o) or archive (.a) file read from r. The new reader may provide
-// additional trailing data beyond the end of the export data.
-func NewReader(r io.Reader) (io.Reader, error) {
- buf := bufio.NewReader(r)
- size, err := gcimporter.FindExportData(buf)
- if err != nil {
- return nil, err
- }
-
- // We were given an archive and found the __.PKGDEF in it.
- // This tells us the size of the export data, and we don't
- // need to return the entire file.
- return &io.LimitedReader{
- R: buf,
- N: size,
- }, nil
-}
-
-// readAll works the same way as io.ReadAll, but avoids allocations and copies
-// by preallocating a byte slice of the necessary size if the size is known up
-// front. This is always possible when the input is an archive. In that case,
-// NewReader will return the known size using an io.LimitedReader.
-func readAll(r io.Reader) ([]byte, error) {
- if lr, ok := r.(*io.LimitedReader); ok {
- data := make([]byte, lr.N)
- _, err := io.ReadFull(lr, data)
- return data, err
- }
- return io.ReadAll(r)
-}
-
-// Read reads export data from in, decodes it, and returns type
-// information for the package.
-//
-// Read is capable of reading export data produced by [Write] at the
-// same source code version, or by the last two Go releases (plus tip)
-// of the standard Go compiler. Reading files from older compilers may
-// produce an error.
-//
-// The package path (effectively its linker symbol prefix) is
-// specified by path, since unlike the package name, this information
-// may not be recorded in the export data.
-//
-// File position information is added to fset.
-//
-// Read may inspect and add to the imports map to ensure that references
-// within the export data to other packages are consistent. The caller
-// must ensure that imports[path] does not exist, or exists but is
-// incomplete (see types.Package.Complete), and Read inserts the
-// resulting package into this map entry.
-//
-// On return, the state of the reader is undefined.
-func Read(in io.Reader, fset *token.FileSet, imports map[string]*types.Package, path string) (*types.Package, error) {
- data, err := readAll(in)
- if err != nil {
- return nil, fmt.Errorf("reading export data for %q: %v", path, err)
- }
-
- if bytes.HasPrefix(data, []byte("!<arch>")) {
- return nil, fmt.Errorf("can't read export data for %q directly from an archive file (call gcexportdata.NewReader first to extract export data)", path)
- }
-
- // The indexed export format starts with an 'i'; the older
- // binary export format starts with a 'c', 'd', or 'v'
- // (from "version"). Select appropriate importer.
- if len(data) > 0 {
- switch data[0] {
- case 'v', 'c', 'd':
- // binary, produced by cmd/compile till go1.10
- return nil, fmt.Errorf("binary (%c) import format is no longer supported", data[0])
-
- case 'i':
- // indexed, produced by cmd/compile till go1.19,
- // and also by [Write].
- //
- // If proposal #69491 is accepted, go/types
- // serialization will be implemented by
- // types.Export, to which Write would eventually
- // delegate (explicitly dropping any pretence at
- // inter-version Write-Read compatibility).
- // This [Read] function would delegate to types.Import
- // when it detects that the file was produced by Export.
- _, pkg, err := gcimporter.IImportData(fset, imports, data[1:], path)
- return pkg, err
-
- case 'u':
- // unified, produced by cmd/compile since go1.20
- _, pkg, err := gcimporter.UImportData(fset, imports, data[1:], path)
- return pkg, err
-
- default:
- l := len(data)
- if l > 10 {
- l = 10
- }
- return nil, fmt.Errorf("unexpected export data with prefix %q for path %s", string(data[:l]), path)
- }
- }
- return nil, fmt.Errorf("empty export data for %s", path)
-}
-
-// Write writes encoded type information for the specified package to out.
-// The FileSet provides file position information for named objects.
-func Write(out io.Writer, fset *token.FileSet, pkg *types.Package) error {
- if _, err := io.WriteString(out, "i"); err != nil {
- return err
- }
- return gcimporter.IExportData(out, fset, pkg)
-}
-
-// ReadBundle reads an export bundle from in, decodes it, and returns type
-// information for the packages.
-// File position information is added to fset.
-//
-// ReadBundle may inspect and add to the imports map to ensure that references
-// within the export bundle to other packages are consistent.
-//
-// On return, the state of the reader is undefined.
-//
-// Experimental: This API is experimental and may change in the future.
-func ReadBundle(in io.Reader, fset *token.FileSet, imports map[string]*types.Package) ([]*types.Package, error) {
- data, err := readAll(in)
- if err != nil {
- return nil, fmt.Errorf("reading export bundle: %v", err)
- }
- return gcimporter.IImportBundle(fset, imports, data)
-}
-
-// WriteBundle writes encoded type information for the specified packages to out.
-// The FileSet provides file position information for named objects.
-//
-// Experimental: This API is experimental and may change in the future.
-func WriteBundle(out io.Writer, fset *token.FileSet, pkgs []*types.Package) error {
- return gcimporter.IExportBundle(out, fset, pkgs)
-}
diff --git a/vendor/golang.org/x/tools/go/gcexportdata/importer.go b/vendor/golang.org/x/tools/go/gcexportdata/importer.go
deleted file mode 100644
index 37a7247e2..000000000
--- a/vendor/golang.org/x/tools/go/gcexportdata/importer.go
+++ /dev/null
@@ -1,75 +0,0 @@
-// Copyright 2016 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.
-
-package gcexportdata
-
-import (
- "fmt"
- "go/token"
- "go/types"
- "os"
-)
-
-// NewImporter returns a new instance of the types.Importer interface
-// that reads type information from export data files written by gc.
-// The Importer also satisfies types.ImporterFrom.
-//
-// Export data files are located using "go build" workspace conventions
-// and the build.Default context.
-//
-// Use this importer instead of go/importer.For("gc", ...) to avoid the
-// version-skew problems described in the documentation of this package,
-// or to control the FileSet or access the imports map populated during
-// package loading.
-//
-// Deprecated: Use the higher-level API in golang.org/x/tools/go/packages,
-// which is more efficient.
-func NewImporter(fset *token.FileSet, imports map[string]*types.Package) types.ImporterFrom {
- return importer{fset, imports}
-}
-
-type importer struct {
- fset *token.FileSet
- imports map[string]*types.Package
-}
-
-func (imp importer) Import(importPath string) (*types.Package, error) {
- return imp.ImportFrom(importPath, "", 0)
-}
-
-func (imp importer) ImportFrom(importPath, srcDir string, mode types.ImportMode) (_ *types.Package, err error) {
- filename, path := Find(importPath, srcDir)
- if filename == "" {
- if importPath == "unsafe" {
- // Even for unsafe, call Find first in case
- // the package was vendored.
- return types.Unsafe, nil
- }
- return nil, fmt.Errorf("can't find import: %s", importPath)
- }
-
- if pkg, ok := imp.imports[path]; ok && pkg.Complete() {
- return pkg, nil // cache hit
- }
-
- // open file
- f, err := os.Open(filename)
- if err != nil {
- return nil, err
- }
- defer func() {
- f.Close()
- if err != nil {
- // add file name to error
- err = fmt.Errorf("reading export data: %s: %v", filename, err)
- }
- }()
-
- r, err := NewReader(f)
- if err != nil {
- return nil, err
- }
-
- return Read(r, imp.fset, imp.imports, path)
-}
diff --git a/vendor/golang.org/x/tools/go/packages/doc.go b/vendor/golang.org/x/tools/go/packages/doc.go
deleted file mode 100644
index f1931d10e..000000000
--- a/vendor/golang.org/x/tools/go/packages/doc.go
+++ /dev/null
@@ -1,251 +0,0 @@
-// Copyright 2018 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.
-
-/*
-Package packages loads Go packages for inspection and analysis.
-
-The [Load] function takes as input a list of patterns and returns a
-list of [Package] values describing individual packages matched by those
-patterns.
-A [Config] specifies configuration options, the most important of which is
-the [LoadMode], which controls the amount of detail in the loaded packages.
-
-Load passes most patterns directly to the underlying build tool.
-The default build tool is the go command.
-Its supported patterns are described at
-https://pkg.go.dev/cmd/go#hdr-Package_lists_and_patterns.
-Other build systems may be supported by providing a "driver";
-see [The driver protocol].
-
-All patterns with the prefix "query=", where query is a
-non-empty string of letters from [a-z], are reserved and may be
-interpreted as query operators.
-
-Two query operators are currently supported: "file" and "pattern".
-
-The query "file=path/to/file.go" matches the package or packages enclosing
-the Go source file path/to/file.go. For example "file=~/go/src/fmt/print.go"
-might return the packages "fmt" and "fmt [fmt.test]".
-
-The query "pattern=string" causes "string" to be passed directly to
-the underlying build tool. In most cases this is unnecessary,
-but an application can use Load("pattern=" + x) as an escaping mechanism
-to ensure that x is not interpreted as a query operator if it contains '='.
-
-All other query operators are reserved for future use and currently
-cause Load to report an error.
-
-The Package struct provides basic information about the package, including
-
- - ID, a unique identifier for the package in the returned set;
- - GoFiles, the names of the package's Go source files;
- - Imports, a map from source import strings to the Packages they name;
- - Types, the type information for the package's exported symbols;
- - Syntax, the parsed syntax trees for the package's source code; and
- - TypesInfo, the result of a complete type-check of the package syntax trees.
-
-(See the documentation for type Package for the complete list of fields
-and more detailed descriptions.)
-
-For example,
-
- Load(nil, "bytes", "unicode...")
-
-returns four Package structs describing the standard library packages
-bytes, unicode, unicode/utf16, and unicode/utf8. Note that one pattern
-can match multiple packages and that a package might be matched by
-multiple patterns: in general it is not possible to determine which
-packages correspond to which patterns.
-
-Note that the list returned by Load contains only the packages matched
-by the patterns. Their dependencies can be found by walking the import
-graph using the Imports fields.
-
-The Load function can be configured by passing a pointer to a Config as
-the first argument. A nil Config is equivalent to the zero Config, which
-causes Load to run in [LoadFiles] mode, collecting minimal information.
-See the documentation for type Config for details.
-
-As noted earlier, the Config.Mode controls the amount of detail
-reported about the loaded packages. See the documentation for type LoadMode
-for details.
-
-Most tools should pass their command-line arguments (after any flags)
-uninterpreted to Load, so that it can interpret them
-according to the conventions of the underlying build system.
-
-See the Example function for typical usage.
-
-# The driver protocol
-
-Load may be used to load Go packages even in Go projects that use
-alternative build systems, by installing an appropriate "driver"
-program for the build system and specifying its location in the
-GOPACKAGESDRIVER environment variable.
-For example,
-https://github.com/bazelbuild/rules_go/wiki/Editor-and-tool-integration
-explains how to use the driver for Bazel.
-
-The driver program is responsible for interpreting patterns in its
-preferred notation and reporting information about the packages that
-those patterns identify. Drivers must also support the special "file="
-and "pattern=" patterns described above.
-
-The patterns are provided as positional command-line arguments. A
-JSON-encoded [DriverRequest] message providing additional information
-is written to the driver's standard input. The driver must write a
-JSON-encoded [DriverResponse] message to its standard output. (This
-message differs from the JSON schema produced by 'go list'.)
-
-The value of the PWD environment variable seen by the driver process
-is the preferred name of its working directory. (The working directory
-may have other aliases due to symbolic links; see the comment on the
-Dir field of [exec.Cmd] for related information.)
-When the driver process emits in its response the name of a file
-that is a descendant of this directory, it must use an absolute path
-that has the value of PWD as a prefix, to ensure that the returned
-filenames satisfy the original query.
-*/
-package packages // import "golang.org/x/tools/go/packages"
-
-/*
-
-Motivation and design considerations
-
-The new package's design solves problems addressed by two existing
-packages: go/build, which locates and describes packages, and
-golang.org/x/tools/go/loader, which loads, parses and type-checks them.
-The go/build.Package structure encodes too much of the 'go build' way
-of organizing projects, leaving us in need of a data type that describes a
-package of Go source code independent of the underlying build system.
-We wanted something that works equally well with go build and vgo, and
-also other build systems such as Bazel and Blaze, making it possible to
-construct analysis tools that work in all these environments.
-Tools such as errcheck and staticcheck were essentially unavailable to
-the Go community at Google, and some of Google's internal tools for Go
-are unavailable externally.
-This new package provides a uniform way to obtain package metadata by
-querying each of these build systems, optionally supporting their
-preferred command-line notations for packages, so that tools integrate
-neatly with users' build environments. The Metadata query function
-executes an external query tool appropriate to the current workspace.
-
-Loading packages always returns the complete import graph "all the way down",
-even if all you want is information about a single package, because the query
-mechanisms of all the build systems we currently support ({go,vgo} list, and
-blaze/bazel aspect-based query) cannot provide detailed information
-about one package without visiting all its dependencies too, so there is
-no additional asymptotic cost to providing transitive information.
-(This property might not be true of a hypothetical 5th build system.)
-
-In calls to TypeCheck, all initial packages, and any package that
-transitively depends on one of them, must be loaded from source.
-Consider A->B->C->D->E: if A,C are initial, A,B,C must be loaded from
-source; D may be loaded from export data, and E may not be loaded at all
-(though it's possible that D's export data mentions it, so a
-types.Package may be created for it and exposed.)
-
-The old loader had a feature to suppress type-checking of function
-bodies on a per-package basis, primarily intended to reduce the work of
-obtaining type information for imported packages. Now that imports are
-satisfied by export data, the optimization no longer seems necessary.
-
-Despite some early attempts, the old loader did not exploit export data,
-instead always using the equivalent of WholeProgram mode. This was due
-to the complexity of mixing source and export data packages (now
-resolved by the upward traversal mentioned above), and because export data
-files were nearly always missing or stale. Now that 'go build' supports
-caching, all the underlying build systems can guarantee to produce
-export data in a reasonable (amortized) time.
-
-Test "main" packages synthesized by the build system are now reported as
-first-class packages, avoiding the need for clients (such as go/ssa) to
-reinvent this generation logic.
-
-One way in which go/packages is simpler than the old loader is in its
-treatment of in-package tests. In-package tests are packages that
-consist of all the files of the library under test, plus the test files.
-The old loader constructed in-package tests by a two-phase process of
-mutation called "augmentation": first it would construct and type check
-all the ordinary library packages and type-check the packages that
-depend on them; then it would add more (test) files to the package and
-type-check again. This two-phase approach had four major problems:
-1) in processing the tests, the loader modified the library package,
- leaving no way for a client application to see both the test
- package and the library package; one would mutate into the other.
-2) because test files can declare additional methods on types defined in
- the library portion of the package, the dispatch of method calls in
- the library portion was affected by the presence of the test files.
- This should have been a clue that the packages were logically
- different.
-3) this model of "augmentation" assumed at most one in-package test
- per library package, which is true of projects using 'go build',
- but not other build systems.
-4) because of the two-phase nature of test processing, all packages that
- import the library package had to be processed before augmentation,
- forcing a "one-shot" API and preventing the client from calling Load
- in several times in sequence as is now possible in WholeProgram mode.
- (TypeCheck mode has a similar one-shot restriction for a different reason.)
-
-Early drafts of this package supported "multi-shot" operation.
-Although it allowed clients to make a sequence of calls (or concurrent
-calls) to Load, building up the graph of Packages incrementally,
-it was of marginal value: it complicated the API
-(since it allowed some options to vary across calls but not others),
-it complicated the implementation,
-it cannot be made to work in Types mode, as explained above,
-and it was less efficient than making one combined call (when this is possible).
-Among the clients we have inspected, none made multiple calls to load
-but could not be easily and satisfactorily modified to make only a single call.
-However, applications changes may be required.
-For example, the ssadump command loads the user-specified packages
-and in addition the runtime package. It is tempting to simply append
-"runtime" to the user-provided list, but that does not work if the user
-specified an ad-hoc package such as [a.go b.go].
-Instead, ssadump no longer requests the runtime package,
-but seeks it among the dependencies of the user-specified packages,
-and emits an error if it is not found.
-
-Questions & Tasks
-
-- Add GOARCH/GOOS?
- They are not portable concepts, but could be made portable.
- Our goal has been to allow users to express themselves using the conventions
- of the underlying build system: if the build system honors GOARCH
- during a build and during a metadata query, then so should
- applications built atop that query mechanism.
- Conversely, if the target architecture of the build is determined by
- command-line flags, the application can pass the relevant
- flags through to the build system using a command such as:
- myapp -query_flag="--cpu=amd64" -query_flag="--os=darwin"
- However, this approach is low-level, unwieldy, and non-portable.
- GOOS and GOARCH seem important enough to warrant a dedicated option.
-
-- How should we handle partial failures such as a mixture of good and
- malformed patterns, existing and non-existent packages, successful and
- failed builds, import failures, import cycles, and so on, in a call to
- Load?
-
-- Support bazel, blaze, and go1.10 list, not just go1.11 list.
-
-- Handle (and test) various partial success cases, e.g.
- a mixture of good packages and:
- invalid patterns
- nonexistent packages
- empty packages
- packages with malformed package or import declarations
- unreadable files
- import cycles
- other parse errors
- type errors
- Make sure we record errors at the correct place in the graph.
-
-- Missing packages among initial arguments are not reported.
- Return bogus packages for them, like golist does.
-
-- "undeclared name" errors (for example) are reported out of source file
- order. I suspect this is due to the breadth-first resolution now used
- by go/types. Is that a bug? Discuss with gri.
-
-*/
diff --git a/vendor/golang.org/x/tools/go/packages/external.go b/vendor/golang.org/x/tools/go/packages/external.go
deleted file mode 100644
index 91bd62e83..000000000
--- a/vendor/golang.org/x/tools/go/packages/external.go
+++ /dev/null
@@ -1,153 +0,0 @@
-// Copyright 2018 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.
-
-package packages
-
-// This file defines the protocol that enables an external "driver"
-// tool to supply package metadata in place of 'go list'.
-
-import (
- "bytes"
- "encoding/json"
- "fmt"
- "os"
- "os/exec"
- "slices"
- "strings"
-)
-
-// DriverRequest defines the schema of a request for package metadata
-// from an external driver program. The JSON-encoded DriverRequest
-// message is provided to the driver program's standard input. The
-// query patterns are provided as command-line arguments.
-//
-// See the package documentation for an overview.
-type DriverRequest struct {
- Mode LoadMode `json:"mode"`
-
- // Env specifies the environment the underlying build system should be run in.
- Env []string `json:"env"`
-
- // BuildFlags are flags that should be passed to the underlying build system.
- BuildFlags []string `json:"build_flags"`
-
- // Tests specifies whether the patterns should also return test packages.
- Tests bool `json:"tests"`
-
- // Overlay maps file paths (relative to the driver's working directory)
- // to the contents of overlay files (see Config.Overlay).
- Overlay map[string][]byte `json:"overlay"`
-}
-
-// DriverResponse defines the schema of a response from an external
-// driver program, providing the results of a query for package
-// metadata. The driver program must write a JSON-encoded
-// DriverResponse message to its standard output.
-//
-// See the package documentation for an overview.
-type DriverResponse struct {
- // NotHandled is returned if the request can't be handled by the current
- // driver. If an external driver returns a response with NotHandled, the
- // rest of the DriverResponse is ignored, and go/packages will fallback
- // to the next driver. If go/packages is extended in the future to support
- // lists of multiple drivers, go/packages will fall back to the next driver.
- NotHandled bool
-
- // Compiler and Arch are the arguments pass of types.SizesFor
- // to get a types.Sizes to use when type checking.
- Compiler string
- Arch string
-
- // Roots is the set of package IDs that make up the root packages.
- // We have to encode this separately because when we encode a single package
- // we cannot know if it is one of the roots as that requires knowledge of the
- // graph it is part of.
- Roots []string `json:",omitempty"`
-
- // Packages is the full set of packages in the graph.
- // The packages are not connected into a graph.
- // The Imports if populated will be stubs that only have their ID set.
- // Imports will be connected and then type and syntax information added in a
- // later pass (see refine).
- Packages []*Package
-
- // GoVersion is the minor version number used by the driver
- // (e.g. the go command on the PATH) when selecting .go files.
- // Zero means unknown.
- GoVersion int
-}
-
-// driver is the type for functions that query the build system for the
-// packages named by the patterns.
-type driver func(cfg *Config, patterns []string) (*DriverResponse, error)
-
-// findExternalDriver returns the file path of a tool that supplies
-// the build system package structure, or "" if not found.
-// If GOPACKAGESDRIVER is set in the environment findExternalTool returns its
-// value, otherwise it searches for a binary named gopackagesdriver on the PATH.
-func findExternalDriver(cfg *Config) driver {
- const toolPrefix = "GOPACKAGESDRIVER="
- tool := ""
- for _, env := range cfg.Env {
- if val := strings.TrimPrefix(env, toolPrefix); val != env {
- tool = val
- }
- }
- if tool != "" && tool == "off" {
- return nil
- }
- if tool == "" {
- var err error
- tool, err = exec.LookPath("gopackagesdriver")
- if err != nil {
- return nil
- }
- }
- return func(cfg *Config, patterns []string) (*DriverResponse, error) {
- req, err := json.Marshal(DriverRequest{
- Mode: cfg.Mode,
- Env: cfg.Env,
- BuildFlags: cfg.BuildFlags,
- Tests: cfg.Tests,
- Overlay: cfg.Overlay,
- })
- if err != nil {
- return nil, fmt.Errorf("failed to encode message to driver tool: %v", err)
- }
-
- buf := new(bytes.Buffer)
- stderr := new(bytes.Buffer)
- cmd := exec.CommandContext(cfg.Context, tool, patterns...)
- cmd.Dir = cfg.Dir
- // The cwd gets resolved to the real path. On Darwin, where
- // /tmp is a symlink, this breaks anything that expects the
- // working directory to keep the original path, including the
- // go command when dealing with modules.
- //
- // os.Getwd stdlib has a special feature where if the
- // cwd and the PWD are the same node then it trusts
- // the PWD, so by setting it in the env for the child
- // process we fix up all the paths returned by the go
- // command.
- //
- // (See similar trick in Invocation.run in ../../internal/gocommand/invoke.go)
- cmd.Env = append(slices.Clip(cfg.Env), "PWD="+cfg.Dir)
- cmd.Stdin = bytes.NewReader(req)
- cmd.Stdout = buf
- cmd.Stderr = stderr
-
- if err := cmd.Run(); err != nil {
- return nil, fmt.Errorf("%v: %v: %s", tool, err, cmd.Stderr)
- }
- if len(stderr.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTDRIVERERRORS") != "" {
- fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(cmd), stderr)
- }
-
- var response DriverResponse
- if err := json.Unmarshal(buf.Bytes(), &response); err != nil {
- return nil, err
- }
- return &response, nil
- }
-}
diff --git a/vendor/golang.org/x/tools/go/packages/golist.go b/vendor/golang.org/x/tools/go/packages/golist.go
deleted file mode 100644
index 870271ed5..000000000
--- a/vendor/golang.org/x/tools/go/packages/golist.go
+++ /dev/null
@@ -1,1081 +0,0 @@
-// Copyright 2018 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.
-
-package packages
-
-import (
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "log"
- "os"
- "os/exec"
- "path"
- "path/filepath"
- "reflect"
- "sort"
- "strconv"
- "strings"
- "sync"
- "unicode"
-
- "golang.org/x/tools/internal/gocommand"
- "golang.org/x/tools/internal/packagesinternal"
-)
-
-// debug controls verbose logging.
-var debug, _ = strconv.ParseBool(os.Getenv("GOPACKAGESDEBUG"))
-
-// A goTooOldError reports that the go command
-// found by exec.LookPath is too old to use the new go list behavior.
-type goTooOldError struct {
- error
-}
-
-// responseDeduper wraps a DriverResponse, deduplicating its contents.
-type responseDeduper struct {
- seenRoots map[string]bool
- seenPackages map[string]*Package
- dr *DriverResponse
-}
-
-func newDeduper() *responseDeduper {
- return &responseDeduper{
- dr: &DriverResponse{},
- seenRoots: map[string]bool{},
- seenPackages: map[string]*Package{},
- }
-}
-
-// addAll fills in r with a DriverResponse.
-func (r *responseDeduper) addAll(dr *DriverResponse) {
- for _, pkg := range dr.Packages {
- r.addPackage(pkg)
- }
- for _, root := range dr.Roots {
- r.addRoot(root)
- }
- r.dr.GoVersion = dr.GoVersion
-}
-
-func (r *responseDeduper) addPackage(p *Package) {
- if r.seenPackages[p.ID] != nil {
- return
- }
- r.seenPackages[p.ID] = p
- r.dr.Packages = append(r.dr.Packages, p)
-}
-
-func (r *responseDeduper) addRoot(id string) {
- if r.seenRoots[id] {
- return
- }
- r.seenRoots[id] = true
- r.dr.Roots = append(r.dr.Roots, id)
-}
-
-type golistState struct {
- cfg *Config
- ctx context.Context
-
- runner *gocommand.Runner
-
- // overlay is the JSON file that encodes the Config.Overlay
- // mapping, used by 'go list -overlay=...'.
- overlay string
-
- envOnce sync.Once
- goEnvError error
- goEnv map[string]string
-
- rootsOnce sync.Once
- rootDirsError error
- rootDirs map[string]string
-
- goVersionOnce sync.Once
- goVersionError error
- goVersion int // The X in Go 1.X.
-
- // vendorDirs caches the (non)existence of vendor directories.
- vendorDirs map[string]bool
-}
-
-// getEnv returns Go environment variables. Only specific variables are
-// populated -- computing all of them is slow.
-func (state *golistState) getEnv() (map[string]string, error) {
- state.envOnce.Do(func() {
- var b *bytes.Buffer
- b, state.goEnvError = state.invokeGo("env", "-json", "GOMOD", "GOPATH")
- if state.goEnvError != nil {
- return
- }
-
- state.goEnv = make(map[string]string)
- decoder := json.NewDecoder(b)
- if state.goEnvError = decoder.Decode(&state.goEnv); state.goEnvError != nil {
- return
- }
- })
- return state.goEnv, state.goEnvError
-}
-
-// mustGetEnv is a convenience function that can be used if getEnv has already succeeded.
-func (state *golistState) mustGetEnv() map[string]string {
- env, err := state.getEnv()
- if err != nil {
- panic(fmt.Sprintf("mustGetEnv: %v", err))
- }
- return env
-}
-
-// goListDriver uses the go list command to interpret the patterns and produce
-// the build system package structure.
-// See driver for more details.
-//
-// overlay is the JSON file that encodes the cfg.Overlay
-// mapping, used by 'go list -overlay=...'
-func goListDriver(cfg *Config, runner *gocommand.Runner, overlay string, patterns []string) (_ *DriverResponse, err error) {
- // Make sure that any asynchronous go commands are killed when we return.
- parentCtx := cfg.Context
- if parentCtx == nil {
- parentCtx = context.Background()
- }
- ctx, cancel := context.WithCancel(parentCtx)
- defer cancel()
-
- response := newDeduper()
-
- state := &golistState{
- cfg: cfg,
- ctx: ctx,
- vendorDirs: map[string]bool{},
- overlay: overlay,
- runner: runner,
- }
-
- // Fill in response.Sizes asynchronously if necessary.
- if cfg.Mode&NeedTypesSizes != 0 || cfg.Mode&(NeedTypes|NeedTypesInfo) != 0 {
- errCh := make(chan error)
- go func() {
- compiler, arch, err := getSizesForArgs(ctx, state.cfgInvocation(), runner)
- response.dr.Compiler = compiler
- response.dr.Arch = arch
- errCh <- err
- }()
- defer func() {
- if sizesErr := <-errCh; sizesErr != nil {
- err = sizesErr
- }
- }()
- }
-
- // Determine files requested in contains patterns
- var containFiles []string
- restPatterns := make([]string, 0, len(patterns))
- // Extract file= and other [querytype]= patterns. Report an error if querytype
- // doesn't exist.
-extractQueries:
- for _, pattern := range patterns {
- eqidx := strings.Index(pattern, "=")
- if eqidx < 0 {
- restPatterns = append(restPatterns, pattern)
- } else {
- query, value := pattern[:eqidx], pattern[eqidx+len("="):]
- switch query {
- case "file":
- containFiles = append(containFiles, value)
- case "pattern":
- restPatterns = append(restPatterns, value)
- case "": // not a reserved query
- restPatterns = append(restPatterns, pattern)
- default:
- for _, rune := range query {
- if rune < 'a' || rune > 'z' { // not a reserved query
- restPatterns = append(restPatterns, pattern)
- continue extractQueries
- }
- }
- // Reject all other patterns containing "="
- return nil, fmt.Errorf("invalid query type %q in query pattern %q", query, pattern)
- }
- }
- }
-
- // See if we have any patterns to pass through to go list. Zero initial
- // patterns also requires a go list call, since it's the equivalent of
- // ".".
- if len(restPatterns) > 0 || len(patterns) == 0 {
- dr, err := state.createDriverResponse(restPatterns...)
- if err != nil {
- return nil, err
- }
- response.addAll(dr)
- }
-
- if len(containFiles) != 0 {
- if err := state.runContainsQueries(response, containFiles); err != nil {
- return nil, err
- }
- }
-
- // (We may yet return an error due to defer.)
- return response.dr, nil
-}
-
-func (state *golistState) runContainsQueries(response *responseDeduper, queries []string) error {
- for _, query := range queries {
- // TODO(matloob): Do only one query per directory.
- fdir := filepath.Dir(query)
- // Pass absolute path of directory to go list so that it knows to treat it as a directory,
- // not a package path.
- pattern, err := filepath.Abs(fdir)
- if err != nil {
- return fmt.Errorf("could not determine absolute path of file= query path %q: %v", query, err)
- }
- dirResponse, err := state.createDriverResponse(pattern)
-
- // If there was an error loading the package, or no packages are returned,
- // or the package is returned with errors, try to load the file as an
- // ad-hoc package.
- // Usually the error will appear in a returned package, but may not if we're
- // in module mode and the ad-hoc is located outside a module.
- if err != nil || len(dirResponse.Packages) == 0 || len(dirResponse.Packages) == 1 && len(dirResponse.Packages[0].GoFiles) == 0 &&
- len(dirResponse.Packages[0].Errors) == 1 {
- var queryErr error
- if dirResponse, queryErr = state.adhocPackage(pattern, query); queryErr != nil {
- return err // return the original error
- }
- }
- isRoot := make(map[string]bool, len(dirResponse.Roots))
- for _, root := range dirResponse.Roots {
- isRoot[root] = true
- }
- for _, pkg := range dirResponse.Packages {
- // Add any new packages to the main set
- // We don't bother to filter packages that will be dropped by the changes of roots,
- // that will happen anyway during graph construction outside this function.
- // Over-reporting packages is not a problem.
- response.addPackage(pkg)
- // if the package was not a root one, it cannot have the file
- if !isRoot[pkg.ID] {
- continue
- }
- for _, pkgFile := range pkg.GoFiles {
- if filepath.Base(query) == filepath.Base(pkgFile) {
- response.addRoot(pkg.ID)
- break
- }
- }
- }
- }
- return nil
-}
-
-// adhocPackage attempts to load or construct an ad-hoc package for a given
-// query, if the original call to the driver produced inadequate results.
-func (state *golistState) adhocPackage(pattern, query string) (*DriverResponse, error) {
- response, err := state.createDriverResponse(query)
- if err != nil {
- return nil, err
- }
- // If we get nothing back from `go list`,
- // try to make this file into its own ad-hoc package.
- // TODO(rstambler): Should this check against the original response?
- if len(response.Packages) == 0 {
- response.Packages = append(response.Packages, &Package{
- ID: "command-line-arguments",
- PkgPath: query,
- GoFiles: []string{query},
- CompiledGoFiles: []string{query},
- Imports: make(map[string]*Package),
- })
- response.Roots = append(response.Roots, "command-line-arguments")
- }
- // Handle special cases.
- if len(response.Packages) == 1 {
- // golang/go#33482: If this is a file= query for ad-hoc packages where
- // the file only exists on an overlay, and exists outside of a module,
- // add the file to the package and remove the errors.
- if response.Packages[0].ID == "command-line-arguments" ||
- filepath.ToSlash(response.Packages[0].PkgPath) == filepath.ToSlash(query) {
- if len(response.Packages[0].GoFiles) == 0 {
- filename := filepath.Join(pattern, filepath.Base(query)) // avoid recomputing abspath
- // TODO(matloob): check if the file is outside of a root dir?
- for path := range state.cfg.Overlay {
- if path == filename {
- response.Packages[0].Errors = nil
- response.Packages[0].GoFiles = []string{path}
- response.Packages[0].CompiledGoFiles = []string{path}
- }
- }
- }
- }
- }
- return response, nil
-}
-
-// Fields must match go list;
-// see $GOROOT/src/cmd/go/internal/load/pkg.go.
-type jsonPackage struct {
- ImportPath string
- Dir string
- Name string
- Export string
- GoFiles []string
- CompiledGoFiles []string
- IgnoredGoFiles []string
- IgnoredOtherFiles []string
- EmbedPatterns []string
- EmbedFiles []string
- CFiles []string
- CgoFiles []string
- CXXFiles []string
- MFiles []string
- HFiles []string
- FFiles []string
- SFiles []string
- SwigFiles []string
- SwigCXXFiles []string
- SysoFiles []string
- Imports []string
- ImportMap map[string]string
- Deps []string
- Module *Module
- TestGoFiles []string
- TestImports []string
- XTestGoFiles []string
- XTestImports []string
- ForTest string // q in a "p [q.test]" package, else ""
- DepOnly bool
-
- Error *packagesinternal.PackageError
- DepsErrors []*packagesinternal.PackageError
-}
-
-type jsonPackageError struct {
- ImportStack []string
- Pos string
- Err string
-}
-
-func otherFiles(p *jsonPackage) [][]string {
- return [][]string{p.CFiles, p.CXXFiles, p.MFiles, p.HFiles, p.FFiles, p.SFiles, p.SwigFiles, p.SwigCXXFiles, p.SysoFiles}
-}
-
-// createDriverResponse uses the "go list" command to expand the pattern
-// words and return a response for the specified packages.
-func (state *golistState) createDriverResponse(words ...string) (*DriverResponse, error) {
- // go list uses the following identifiers in ImportPath and Imports:
- //
- // "p" -- importable package or main (command)
- // "q.test" -- q's test executable
- // "p [q.test]" -- variant of p as built for q's test executable
- // "q_test [q.test]" -- q's external test package
- //
- // The packages p that are built differently for a test q.test
- // are q itself, plus any helpers used by the external test q_test,
- // typically including "testing" and all its dependencies.
-
- // Run "go list" for complete
- // information on the specified packages.
- goVersion, err := state.getGoVersion()
- if err != nil {
- return nil, err
- }
- buf, err := state.invokeGo("list", golistargs(state.cfg, words, goVersion)...)
- if err != nil {
- return nil, err
- }
-
- seen := make(map[string]*jsonPackage)
- pkgs := make(map[string]*Package)
- additionalErrors := make(map[string][]Error)
- // Decode the JSON and convert it to Package form.
- response := &DriverResponse{
- GoVersion: goVersion,
- }
- for dec := json.NewDecoder(buf); dec.More(); {
- p := new(jsonPackage)
- if err := dec.Decode(p); err != nil {
- return nil, fmt.Errorf("JSON decoding failed: %v", err)
- }
-
- if p.ImportPath == "" {
- // The documentation for go list says that “[e]rroneous packages will have
- // a non-empty ImportPath”. If for some reason it comes back empty, we
- // prefer to error out rather than silently discarding data or handing
- // back a package without any way to refer to it.
- if p.Error != nil {
- return nil, Error{
- Pos: p.Error.Pos,
- Msg: p.Error.Err,
- }
- }
- return nil, fmt.Errorf("package missing import path: %+v", p)
- }
-
- // Work around https://golang.org/issue/33157:
- // go list -e, when given an absolute path, will find the package contained at
- // that directory. But when no package exists there, it will return a fake package
- // with an error and the ImportPath set to the absolute path provided to go list.
- // Try to convert that absolute path to what its package path would be if it's
- // contained in a known module or GOPATH entry. This will allow the package to be
- // properly "reclaimed" when overlays are processed.
- if filepath.IsAbs(p.ImportPath) && p.Error != nil {
- pkgPath, ok, err := state.getPkgPath(p.ImportPath)
- if err != nil {
- return nil, err
- }
- if ok {
- p.ImportPath = pkgPath
- }
- }
-
- if old, found := seen[p.ImportPath]; found {
- // If one version of the package has an error, and the other doesn't, assume
- // that this is a case where go list is reporting a fake dependency variant
- // of the imported package: When a package tries to invalidly import another
- // package, go list emits a variant of the imported package (with the same
- // import path, but with an error on it, and the package will have a
- // DepError set on it). An example of when this can happen is for imports of
- // main packages: main packages can not be imported, but they may be
- // separately matched and listed by another pattern.
- // See golang.org/issue/36188 for more details.
-
- // The plan is that eventually, hopefully in Go 1.15, the error will be
- // reported on the importing package rather than the duplicate "fake"
- // version of the imported package. Once all supported versions of Go
- // have the new behavior this logic can be deleted.
- // TODO(matloob): delete the workaround logic once all supported versions of
- // Go return the errors on the proper package.
-
- // There should be exactly one version of a package that doesn't have an
- // error.
- if old.Error == nil && p.Error == nil {
- if !reflect.DeepEqual(p, old) {
- return nil, fmt.Errorf("internal error: go list gives conflicting information for package %v", p.ImportPath)
- }
- continue
- }
-
- // Determine if this package's error needs to be bubbled up.
- // This is a hack, and we expect for go list to eventually set the error
- // on the package.
- if old.Error != nil {
- var errkind string
- if strings.Contains(old.Error.Err, "not an importable package") {
- errkind = "not an importable package"
- } else if strings.Contains(old.Error.Err, "use of internal package") && strings.Contains(old.Error.Err, "not allowed") {
- errkind = "use of internal package not allowed"
- }
- if errkind != "" {
- if len(old.Error.ImportStack) < 1 {
- return nil, fmt.Errorf(`internal error: go list gave a %q error with empty import stack`, errkind)
- }
- importingPkg := old.Error.ImportStack[len(old.Error.ImportStack)-1]
- if importingPkg == old.ImportPath {
- // Using an older version of Go which put this package itself on top of import
- // stack, instead of the importer. Look for importer in second from top
- // position.
- if len(old.Error.ImportStack) < 2 {
- return nil, fmt.Errorf(`internal error: go list gave a %q error with an import stack without importing package`, errkind)
- }
- importingPkg = old.Error.ImportStack[len(old.Error.ImportStack)-2]
- }
- additionalErrors[importingPkg] = append(additionalErrors[importingPkg], Error{
- Pos: old.Error.Pos,
- Msg: old.Error.Err,
- Kind: ListError,
- })
- }
- }
-
- // Make sure that if there's a version of the package without an error,
- // that's the one reported to the user.
- if old.Error == nil {
- continue
- }
-
- // This package will replace the old one at the end of the loop.
- }
- seen[p.ImportPath] = p
-
- pkg := &Package{
- Name: p.Name,
- ID: p.ImportPath,
- Dir: p.Dir,
- GoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles),
- CompiledGoFiles: absJoin(p.Dir, p.CompiledGoFiles),
- OtherFiles: absJoin(p.Dir, otherFiles(p)...),
- EmbedFiles: absJoin(p.Dir, p.EmbedFiles),
- EmbedPatterns: absJoin(p.Dir, p.EmbedPatterns),
- IgnoredFiles: absJoin(p.Dir, p.IgnoredGoFiles, p.IgnoredOtherFiles),
- ForTest: p.ForTest,
- depsErrors: p.DepsErrors,
- Module: p.Module,
- }
-
- if (state.cfg.Mode&typecheckCgo) != 0 && len(p.CgoFiles) != 0 {
- if len(p.CompiledGoFiles) > len(p.GoFiles) {
- // We need the cgo definitions, which are in the first
- // CompiledGoFile after the non-cgo ones. This is a hack but there
- // isn't currently a better way to find it. We also need the pure
- // Go files and unprocessed cgo files, all of which are already
- // in pkg.GoFiles.
- cgoTypes := p.CompiledGoFiles[len(p.GoFiles)]
- pkg.CompiledGoFiles = append([]string{cgoTypes}, pkg.GoFiles...)
- } else {
- // golang/go#38990: go list silently fails to do cgo processing
- pkg.CompiledGoFiles = nil
- pkg.Errors = append(pkg.Errors, Error{
- Msg: "go list failed to return CompiledGoFiles. This may indicate failure to perform cgo processing; try building at the command line. See https://golang.org/issue/38990.",
- Kind: ListError,
- })
- }
- }
-
- // Work around https://golang.org/issue/28749:
- // cmd/go puts assembly, C, and C++ files in CompiledGoFiles.
- // Remove files from CompiledGoFiles that are non-go files
- // (or are not files that look like they are from the cache).
- if len(pkg.CompiledGoFiles) > 0 {
- out := pkg.CompiledGoFiles[:0]
- for _, f := range pkg.CompiledGoFiles {
- if ext := filepath.Ext(f); ext != ".go" && ext != "" { // ext == "" means the file is from the cache, so probably cgo-processed file
- continue
- }
- out = append(out, f)
- }
- pkg.CompiledGoFiles = out
- }
-
- // Extract the PkgPath from the package's ID.
- if i := strings.IndexByte(pkg.ID, ' '); i >= 0 {
- pkg.PkgPath = pkg.ID[:i]
- } else {
- pkg.PkgPath = pkg.ID
- }
-
- if pkg.PkgPath == "unsafe" {
- pkg.CompiledGoFiles = nil // ignore fake unsafe.go file (#59929)
- } else if len(pkg.CompiledGoFiles) == 0 {
- // Work around for pre-go.1.11 versions of go list.
- // TODO(matloob): they should be handled by the fallback.
- // Can we delete this?
- pkg.CompiledGoFiles = pkg.GoFiles
- }
-
- // Assume go list emits only absolute paths for Dir.
- if p.Dir != "" && !filepath.IsAbs(p.Dir) {
- log.Fatalf("internal error: go list returned non-absolute Package.Dir: %s", p.Dir)
- }
-
- if p.Export != "" && !filepath.IsAbs(p.Export) {
- pkg.ExportFile = filepath.Join(p.Dir, p.Export)
- } else {
- pkg.ExportFile = p.Export
- }
-
- // imports
- //
- // Imports contains the IDs of all imported packages.
- // ImportsMap records (path, ID) only where they differ.
- ids := make(map[string]bool)
- for _, id := range p.Imports {
- ids[id] = true
- }
- pkg.Imports = make(map[string]*Package)
- for path, id := range p.ImportMap {
- pkg.Imports[path] = &Package{ID: id} // non-identity import
- delete(ids, id)
- }
- for id := range ids {
- if id == "C" {
- continue
- }
-
- pkg.Imports[id] = &Package{ID: id} // identity import
- }
- if !p.DepOnly {
- response.Roots = append(response.Roots, pkg.ID)
- }
-
- // Temporary work-around for golang/go#39986. Parse filenames out of
- // error messages. This happens if there are unrecoverable syntax
- // errors in the source, so we can't match on a specific error message.
- //
- // TODO(rfindley): remove this heuristic, in favor of considering
- // InvalidGoFiles from the list driver.
- if err := p.Error; err != nil && state.shouldAddFilenameFromError(p) {
- addFilenameFromPos := func(pos string) bool {
- split := strings.Split(pos, ":")
- if len(split) < 1 {
- return false
- }
- filename := strings.TrimSpace(split[0])
- if filename == "" {
- return false
- }
- if !filepath.IsAbs(filename) {
- filename = filepath.Join(state.cfg.Dir, filename)
- }
- info, _ := os.Stat(filename)
- if info == nil {
- return false
- }
- pkg.CompiledGoFiles = append(pkg.CompiledGoFiles, filename)
- pkg.GoFiles = append(pkg.GoFiles, filename)
- return true
- }
- found := addFilenameFromPos(err.Pos)
- // In some cases, go list only reports the error position in the
- // error text, not the error position. One such case is when the
- // file's package name is a keyword (see golang.org/issue/39763).
- if !found {
- addFilenameFromPos(err.Err)
- }
- }
-
- if p.Error != nil {
- msg := strings.TrimSpace(p.Error.Err) // Trim to work around golang.org/issue/32363.
- // Address golang.org/issue/35964 by appending import stack to error message.
- if msg == "import cycle not allowed" && len(p.Error.ImportStack) != 0 {
- msg += fmt.Sprintf(": import stack: %v", p.Error.ImportStack)
- }
- pkg.Errors = append(pkg.Errors, Error{
- Pos: p.Error.Pos,
- Msg: msg,
- Kind: ListError,
- })
- }
-
- pkgs[pkg.ID] = pkg
- }
-
- for id, errs := range additionalErrors {
- if p, ok := pkgs[id]; ok {
- p.Errors = append(p.Errors, errs...)
- }
- }
- for _, pkg := range pkgs {
- response.Packages = append(response.Packages, pkg)
- }
- sort.Slice(response.Packages, func(i, j int) bool { return response.Packages[i].ID < response.Packages[j].ID })
-
- return response, nil
-}
-
-func (state *golistState) shouldAddFilenameFromError(p *jsonPackage) bool {
- if len(p.GoFiles) > 0 || len(p.CompiledGoFiles) > 0 {
- return false
- }
-
- goV, err := state.getGoVersion()
- if err != nil {
- return false
- }
-
- // On Go 1.14 and earlier, only add filenames from errors if the import stack is empty.
- // The import stack behaves differently for these versions than newer Go versions.
- if goV < 15 {
- return len(p.Error.ImportStack) == 0
- }
-
- // On Go 1.15 and later, only parse filenames out of error if there's no import stack,
- // or the current package is at the top of the import stack. This is not guaranteed
- // to work perfectly, but should avoid some cases where files in errors don't belong to this
- // package.
- return len(p.Error.ImportStack) == 0 || p.Error.ImportStack[len(p.Error.ImportStack)-1] == p.ImportPath
-}
-
-// getGoVersion returns the effective minor version of the go command.
-func (state *golistState) getGoVersion() (int, error) {
- state.goVersionOnce.Do(func() {
- state.goVersion, state.goVersionError = gocommand.GoVersion(state.ctx, state.cfgInvocation(), state.runner)
- })
- return state.goVersion, state.goVersionError
-}
-
-// getPkgPath finds the package path of a directory if it's relative to a root
-// directory.
-func (state *golistState) getPkgPath(dir string) (string, bool, error) {
- absDir, err := filepath.Abs(dir)
- if err != nil {
- return "", false, err
- }
- roots, err := state.determineRootDirs()
- if err != nil {
- return "", false, err
- }
-
- for rdir, rpath := range roots {
- // Make sure that the directory is in the module,
- // to avoid creating a path relative to another module.
- if !strings.HasPrefix(absDir, rdir) {
- continue
- }
- // TODO(matloob): This doesn't properly handle symlinks.
- r, err := filepath.Rel(rdir, dir)
- if err != nil {
- continue
- }
- if rpath != "" {
- // We choose only one root even though the directory even it can belong in multiple modules
- // or GOPATH entries. This is okay because we only need to work with absolute dirs when a
- // file is missing from disk, for instance when gopls calls go/packages in an overlay.
- // Once the file is saved, gopls, or the next invocation of the tool will get the correct
- // result straight from golist.
- // TODO(matloob): Implement module tiebreaking?
- return path.Join(rpath, filepath.ToSlash(r)), true, nil
- }
- return filepath.ToSlash(r), true, nil
- }
- return "", false, nil
-}
-
-// absJoin absolutizes and flattens the lists of files.
-func absJoin(dir string, fileses ...[]string) (res []string) {
- for _, files := range fileses {
- for _, file := range files {
- if !filepath.IsAbs(file) {
- file = filepath.Join(dir, file)
- }
- res = append(res, file)
- }
- }
- return res
-}
-
-func jsonFlag(cfg *Config, goVersion int) string {
- if goVersion < 19 {
- return "-json"
- }
- var fields []string
- added := make(map[string]bool)
- addFields := func(fs ...string) {
- for _, f := range fs {
- if !added[f] {
- added[f] = true
- fields = append(fields, f)
- }
- }
- }
- addFields("Name", "ImportPath", "Error") // These fields are always needed
- if cfg.Mode&NeedFiles != 0 || cfg.Mode&(NeedTypes|NeedTypesInfo) != 0 {
- addFields("Dir", "GoFiles", "IgnoredGoFiles", "IgnoredOtherFiles", "CFiles",
- "CgoFiles", "CXXFiles", "MFiles", "HFiles", "FFiles", "SFiles",
- "SwigFiles", "SwigCXXFiles", "SysoFiles")
- if cfg.Tests {
- addFields("TestGoFiles", "XTestGoFiles")
- }
- }
- if cfg.Mode&(NeedTypes|NeedTypesInfo) != 0 {
- // CompiledGoFiles seems to be required for the test case TestCgoNoSyntax,
- // even when -compiled isn't passed in.
- // TODO(#52435): Should we make the test ask for -compiled, or automatically
- // request CompiledGoFiles in certain circumstances?
- addFields("Dir", "CompiledGoFiles")
- }
- if cfg.Mode&NeedCompiledGoFiles != 0 {
- addFields("Dir", "CompiledGoFiles", "Export")
- }
- if cfg.Mode&NeedImports != 0 {
- // When imports are requested, DepOnly is used to distinguish between packages
- // explicitly requested and transitive imports of those packages.
- addFields("DepOnly", "Imports", "ImportMap")
- if cfg.Tests {
- addFields("TestImports", "XTestImports")
- }
- }
- if cfg.Mode&NeedDeps != 0 {
- addFields("DepOnly")
- }
- if usesExportData(cfg) {
- // Request Dir in the unlikely case Export is not absolute.
- addFields("Dir", "Export")
- }
- if cfg.Mode&NeedForTest != 0 {
- addFields("ForTest")
- }
- if cfg.Mode&needInternalDepsErrors != 0 {
- addFields("DepsErrors")
- }
- if cfg.Mode&NeedModule != 0 {
- addFields("Module")
- }
- if cfg.Mode&NeedEmbedFiles != 0 {
- addFields("EmbedFiles")
- }
- if cfg.Mode&NeedEmbedPatterns != 0 {
- addFields("EmbedPatterns")
- }
- return "-json=" + strings.Join(fields, ",")
-}
-
-func golistargs(cfg *Config, words []string, goVersion int) []string {
- const findFlags = NeedImports | NeedTypes | NeedSyntax | NeedTypesInfo
- fullargs := []string{
- "-e", jsonFlag(cfg, goVersion),
- fmt.Sprintf("-compiled=%t", cfg.Mode&(NeedCompiledGoFiles|NeedSyntax|NeedTypes|NeedTypesInfo|NeedTypesSizes) != 0),
- fmt.Sprintf("-test=%t", cfg.Tests),
- fmt.Sprintf("-export=%t", usesExportData(cfg)),
- fmt.Sprintf("-deps=%t", cfg.Mode&NeedImports != 0),
- // go list doesn't let you pass -test and -find together,
- // probably because you'd just get the TestMain.
- fmt.Sprintf("-find=%t", !cfg.Tests && cfg.Mode&findFlags == 0 && !usesExportData(cfg)),
- }
-
- // golang/go#60456: with go1.21 and later, go list serves pgo variants, which
- // can be costly to compute and may result in redundant processing for the
- // caller. Disable these variants. If someone wants to add e.g. a NeedPGO
- // mode flag, that should be a separate proposal.
- if goVersion >= 21 {
- fullargs = append(fullargs, "-pgo=off")
- }
-
- fullargs = append(fullargs, cfg.BuildFlags...)
- fullargs = append(fullargs, "--")
- fullargs = append(fullargs, words...)
- return fullargs
-}
-
-// cfgInvocation returns an Invocation that reflects cfg's settings.
-func (state *golistState) cfgInvocation() gocommand.Invocation {
- cfg := state.cfg
- return gocommand.Invocation{
- BuildFlags: cfg.BuildFlags,
- ModFile: cfg.modFile,
- ModFlag: cfg.modFlag,
- CleanEnv: cfg.Env != nil,
- Env: cfg.Env,
- Logf: cfg.Logf,
- WorkingDir: cfg.Dir,
- Overlay: state.overlay,
- }
-}
-
-// invokeGo returns the stdout of a go command invocation.
-func (state *golistState) invokeGo(verb string, args ...string) (*bytes.Buffer, error) {
- cfg := state.cfg
-
- inv := state.cfgInvocation()
- inv.Verb = verb
- inv.Args = args
-
- stdout, stderr, friendlyErr, err := state.runner.RunRaw(cfg.Context, inv)
- if err != nil {
- // Check for 'go' executable not being found.
- if ee, ok := err.(*exec.Error); ok && ee.Err == exec.ErrNotFound {
- return nil, fmt.Errorf("'go list' driver requires 'go', but %s", exec.ErrNotFound)
- }
-
- exitErr, ok := err.(*exec.ExitError)
- if !ok {
- // Catastrophic error:
- // - context cancellation
- return nil, fmt.Errorf("couldn't run 'go': %w", err)
- }
-
- // Old go version?
- if strings.Contains(stderr.String(), "flag provided but not defined") {
- return nil, goTooOldError{fmt.Errorf("unsupported version of go: %s: %s", exitErr, stderr)}
- }
-
- // Related to #24854
- if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "unexpected directory layout") {
- return nil, friendlyErr
- }
-
- // Return an error if 'go list' failed due to missing tools in
- // $GOROOT/pkg/tool/$GOOS_$GOARCH (#69606).
- if len(stderr.String()) > 0 && strings.Contains(stderr.String(), `go: no such tool`) {
- return nil, friendlyErr
- }
-
- // Is there an error running the C compiler in cgo? This will be reported in the "Error" field
- // and should be suppressed by go list -e.
- //
- // This condition is not perfect yet because the error message can include other error messages than runtime/cgo.
- isPkgPathRune := func(r rune) bool {
- // From https://golang.org/ref/spec#Import_declarations:
- // Implementation restriction: A compiler may restrict ImportPaths to non-empty strings
- // using only characters belonging to Unicode's L, M, N, P, and S general categories
- // (the Graphic characters without spaces) and may also exclude the
- // characters !"#$%&'()*,:;<=>?[\]^`{|} and the Unicode replacement character U+FFFD.
- return unicode.IsOneOf([]*unicode.RangeTable{unicode.L, unicode.M, unicode.N, unicode.P, unicode.S}, r) &&
- !strings.ContainsRune("!\"#$%&'()*,:;<=>?[\\]^`{|}\uFFFD", r)
- }
- // golang/go#36770: Handle case where cmd/go prints module download messages before the error.
- msg := stderr.String()
- for strings.HasPrefix(msg, "go: downloading") {
- msg = msg[strings.IndexRune(msg, '\n')+1:]
- }
- if len(stderr.String()) > 0 && strings.HasPrefix(stderr.String(), "# ") {
- msg := msg[len("# "):]
- if strings.HasPrefix(strings.TrimLeftFunc(msg, isPkgPathRune), "\n") {
- return stdout, nil
- }
- // Treat pkg-config errors as a special case (golang.org/issue/36770).
- if strings.HasPrefix(msg, "pkg-config") {
- return stdout, nil
- }
- }
-
- // This error only appears in stderr. See golang.org/cl/166398 for a fix in go list to show
- // the error in the Err section of stdout in case -e option is provided.
- // This fix is provided for backwards compatibility.
- if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "named files must be .go files") {
- output := fmt.Sprintf(`{"ImportPath": "command-line-arguments","Incomplete": true,"Error": {"Pos": "","Err": %q}}`,
- strings.Trim(stderr.String(), "\n"))
- return bytes.NewBufferString(output), nil
- }
-
- // Similar to the previous error, but currently lacks a fix in Go.
- if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "named files must all be in one directory") {
- output := fmt.Sprintf(`{"ImportPath": "command-line-arguments","Incomplete": true,"Error": {"Pos": "","Err": %q}}`,
- strings.Trim(stderr.String(), "\n"))
- return bytes.NewBufferString(output), nil
- }
-
- // Backwards compatibility for Go 1.11 because 1.12 and 1.13 put the directory in the ImportPath.
- // If the package doesn't exist, put the absolute path of the directory into the error message,
- // as Go 1.13 list does.
- const noSuchDirectory = "no such directory"
- if len(stderr.String()) > 0 && strings.Contains(stderr.String(), noSuchDirectory) {
- errstr := stderr.String()
- abspath := strings.TrimSpace(errstr[strings.Index(errstr, noSuchDirectory)+len(noSuchDirectory):])
- output := fmt.Sprintf(`{"ImportPath": %q,"Incomplete": true,"Error": {"Pos": "","Err": %q}}`,
- abspath, strings.Trim(stderr.String(), "\n"))
- return bytes.NewBufferString(output), nil
- }
-
- // Workaround for #29280: go list -e has incorrect behavior when an ad-hoc package doesn't exist.
- // Note that the error message we look for in this case is different that the one looked for above.
- if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "no such file or directory") {
- output := fmt.Sprintf(`{"ImportPath": "command-line-arguments","Incomplete": true,"Error": {"Pos": "","Err": %q}}`,
- strings.Trim(stderr.String(), "\n"))
- return bytes.NewBufferString(output), nil
- }
-
- // Workaround for #34273. go list -e with GO111MODULE=on has incorrect behavior when listing a
- // directory outside any module.
- if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "outside available modules") {
- output := fmt.Sprintf(`{"ImportPath": %q,"Incomplete": true,"Error": {"Pos": "","Err": %q}}`,
- // TODO(matloob): command-line-arguments isn't correct here.
- "command-line-arguments", strings.Trim(stderr.String(), "\n"))
- return bytes.NewBufferString(output), nil
- }
-
- // Another variation of the previous error
- if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "outside module root") {
- output := fmt.Sprintf(`{"ImportPath": %q,"Incomplete": true,"Error": {"Pos": "","Err": %q}}`,
- // TODO(matloob): command-line-arguments isn't correct here.
- "command-line-arguments", strings.Trim(stderr.String(), "\n"))
- return bytes.NewBufferString(output), nil
- }
-
- // Workaround for an instance of golang.org/issue/26755: go list -e will return a non-zero exit
- // status if there's a dependency on a package that doesn't exist. But it should return
- // a zero exit status and set an error on that package.
- if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "no Go files in") {
- // Don't clobber stdout if `go list` actually returned something.
- if len(stdout.String()) > 0 {
- return stdout, nil
- }
- // try to extract package name from string
- stderrStr := stderr.String()
- var importPath string
- colon := strings.Index(stderrStr, ":")
- if colon > 0 && strings.HasPrefix(stderrStr, "go build ") {
- importPath = stderrStr[len("go build "):colon]
- }
- output := fmt.Sprintf(`{"ImportPath": %q,"Incomplete": true,"Error": {"Pos": "","Err": %q}}`,
- importPath, strings.Trim(stderrStr, "\n"))
- return bytes.NewBufferString(output), nil
- }
-
- // Export mode entails a build.
- // If that build fails, errors appear on stderr
- // (despite the -e flag) and the Export field is blank.
- // Do not fail in that case.
- // The same is true if an ad-hoc package given to go list doesn't exist.
- // TODO(matloob): Remove these once we can depend on go list to exit with a zero status with -e even when
- // packages don't exist or a build fails.
- if !usesExportData(cfg) && !containsGoFile(args) {
- return nil, friendlyErr
- }
- }
- return stdout, nil
-}
-
-func containsGoFile(s []string) bool {
- for _, f := range s {
- if strings.HasSuffix(f, ".go") {
- return true
- }
- }
- return false
-}
-
-func cmdDebugStr(cmd *exec.Cmd) string {
- env := make(map[string]string)
- for _, kv := range cmd.Env {
- split := strings.SplitN(kv, "=", 2)
- k, v := split[0], split[1]
- env[k] = v
- }
-
- var args []string
- for _, arg := range cmd.Args {
- quoted := strconv.Quote(arg)
- if quoted[1:len(quoted)-1] != arg || strings.Contains(arg, " ") {
- args = append(args, quoted)
- } else {
- args = append(args, arg)
- }
- }
- return fmt.Sprintf("GOROOT=%v GOPATH=%v GO111MODULE=%v GOPROXY=%v PWD=%v %v", env["GOROOT"], env["GOPATH"], env["GO111MODULE"], env["GOPROXY"], env["PWD"], strings.Join(args, " "))
-}
-
-// getSizesForArgs queries 'go list' for the appropriate
-// Compiler and GOARCH arguments to pass to [types.SizesFor].
-func getSizesForArgs(ctx context.Context, inv gocommand.Invocation, gocmdRunner *gocommand.Runner) (string, string, error) {
- inv.Verb = "list"
- inv.Args = []string{"-f", "{{context.GOARCH}} {{context.Compiler}}", "--", "unsafe"}
- stdout, stderr, friendlyErr, rawErr := gocmdRunner.RunRaw(ctx, inv)
- var goarch, compiler string
- if rawErr != nil {
- rawErrMsg := rawErr.Error()
- if strings.Contains(rawErrMsg, "cannot find main module") ||
- strings.Contains(rawErrMsg, "go.mod file not found") {
- // User's running outside of a module.
- // All bets are off. Get GOARCH and guess compiler is gc.
- // TODO(matloob): Is this a problem in practice?
- inv.Verb = "env"
- inv.Args = []string{"GOARCH"}
- envout, enverr := gocmdRunner.Run(ctx, inv)
- if enverr != nil {
- return "", "", enverr
- }
- goarch = strings.TrimSpace(envout.String())
- compiler = "gc"
- } else if friendlyErr != nil {
- return "", "", friendlyErr
- } else {
- // This should be unreachable, but be defensive
- // in case RunRaw's error results are inconsistent.
- return "", "", rawErr
- }
- } else {
- fields := strings.Fields(stdout.String())
- if len(fields) < 2 {
- return "", "", fmt.Errorf("could not parse GOARCH and Go compiler in format \"<GOARCH> <compiler>\":\nstdout: <<%s>>\nstderr: <<%s>>",
- stdout.String(), stderr.String())
- }
- goarch = fields[0]
- compiler = fields[1]
- }
- return compiler, goarch, nil
-}
diff --git a/vendor/golang.org/x/tools/go/packages/golist_overlay.go b/vendor/golang.org/x/tools/go/packages/golist_overlay.go
deleted file mode 100644
index d823c474a..000000000
--- a/vendor/golang.org/x/tools/go/packages/golist_overlay.go
+++ /dev/null
@@ -1,83 +0,0 @@
-// Copyright 2018 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.
-
-package packages
-
-import (
- "encoding/json"
- "path/filepath"
-
- "golang.org/x/tools/internal/gocommand"
-)
-
-// determineRootDirs returns a mapping from absolute directories that could
-// contain code to their corresponding import path prefixes.
-func (state *golistState) determineRootDirs() (map[string]string, error) {
- env, err := state.getEnv()
- if err != nil {
- return nil, err
- }
- if env["GOMOD"] != "" {
- state.rootsOnce.Do(func() {
- state.rootDirs, state.rootDirsError = state.determineRootDirsModules()
- })
- } else {
- state.rootsOnce.Do(func() {
- state.rootDirs, state.rootDirsError = state.determineRootDirsGOPATH()
- })
- }
- return state.rootDirs, state.rootDirsError
-}
-
-func (state *golistState) determineRootDirsModules() (map[string]string, error) {
- // List all of the modules--the first will be the directory for the main
- // module. Any replaced modules will also need to be treated as roots.
- // Editing files in the module cache isn't a great idea, so we don't
- // plan to ever support that.
- out, err := state.invokeGo("list", "-m", "-json", "all")
- if err != nil {
- // 'go list all' will fail if we're outside of a module and
- // GO111MODULE=on. Try falling back without 'all'.
- var innerErr error
- out, innerErr = state.invokeGo("list", "-m", "-json")
- if innerErr != nil {
- return nil, err
- }
- }
- roots := map[string]string{}
- modules := map[string]string{}
- var i int
- for dec := json.NewDecoder(out); dec.More(); {
- mod := new(gocommand.ModuleJSON)
- if err := dec.Decode(mod); err != nil {
- return nil, err
- }
- if mod.Dir != "" && mod.Path != "" {
- // This is a valid module; add it to the map.
- absDir, err := filepath.Abs(mod.Dir)
- if err != nil {
- return nil, err
- }
- modules[absDir] = mod.Path
- // The first result is the main module.
- if i == 0 || mod.Replace != nil && mod.Replace.Path != "" {
- roots[absDir] = mod.Path
- }
- }
- i++
- }
- return roots, nil
-}
-
-func (state *golistState) determineRootDirsGOPATH() (map[string]string, error) {
- m := map[string]string{}
- for _, dir := range filepath.SplitList(state.mustGetEnv()["GOPATH"]) {
- absDir, err := filepath.Abs(dir)
- if err != nil {
- return nil, err
- }
- m[filepath.Join(absDir, "src")] = ""
- }
- return m, nil
-}
diff --git a/vendor/golang.org/x/tools/go/packages/loadmode_string.go b/vendor/golang.org/x/tools/go/packages/loadmode_string.go
deleted file mode 100644
index 969da4c26..000000000
--- a/vendor/golang.org/x/tools/go/packages/loadmode_string.go
+++ /dev/null
@@ -1,55 +0,0 @@
-// 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.
-
-package packages
-
-import (
- "fmt"
- "strings"
-)
-
-var modes = [...]struct {
- mode LoadMode
- name string
-}{
- {NeedName, "NeedName"},
- {NeedFiles, "NeedFiles"},
- {NeedCompiledGoFiles, "NeedCompiledGoFiles"},
- {NeedImports, "NeedImports"},
- {NeedDeps, "NeedDeps"},
- {NeedExportFile, "NeedExportFile"},
- {NeedTypes, "NeedTypes"},
- {NeedSyntax, "NeedSyntax"},
- {NeedTypesInfo, "NeedTypesInfo"},
- {NeedTypesSizes, "NeedTypesSizes"},
- {NeedForTest, "NeedForTest"},
- {NeedModule, "NeedModule"},
- {NeedEmbedFiles, "NeedEmbedFiles"},
- {NeedEmbedPatterns, "NeedEmbedPatterns"},
-}
-
-func (mode LoadMode) String() string {
- if mode == 0 {
- return "LoadMode(0)"
- }
- var out []string
- // named bits
- for _, item := range modes {
- if (mode & item.mode) != 0 {
- mode ^= item.mode
- out = append(out, item.name)
- }
- }
- // unnamed residue
- if mode != 0 {
- if out == nil {
- return fmt.Sprintf("LoadMode(%#x)", int(mode))
- }
- out = append(out, fmt.Sprintf("%#x", int(mode)))
- }
- if len(out) == 1 {
- return out[0]
- }
- return "(" + strings.Join(out, "|") + ")"
-}
diff --git a/vendor/golang.org/x/tools/go/packages/packages.go b/vendor/golang.org/x/tools/go/packages/packages.go
deleted file mode 100644
index 9dedf9777..000000000
--- a/vendor/golang.org/x/tools/go/packages/packages.go
+++ /dev/null
@@ -1,1564 +0,0 @@
-// Copyright 2018 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.
-
-package packages
-
-// See doc.go for package documentation and implementation notes.
-
-import (
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "go/ast"
- "go/parser"
- "go/scanner"
- "go/token"
- "go/types"
- "log"
- "os"
- "path/filepath"
- "runtime"
- "strings"
- "sync"
- "sync/atomic"
- "time"
-
- "golang.org/x/sync/errgroup"
-
- "golang.org/x/tools/go/gcexportdata"
- "golang.org/x/tools/internal/gocommand"
- "golang.org/x/tools/internal/packagesinternal"
- "golang.org/x/tools/internal/typesinternal"
-)
-
-// A LoadMode controls the amount of detail to return when loading.
-// The bits below can be combined to specify which fields should be
-// filled in the result packages.
-//
-// The zero value is a special case, equivalent to combining
-// the NeedName, NeedFiles, and NeedCompiledGoFiles bits.
-//
-// ID and Errors (if present) will always be filled.
-// [Load] may return more information than requested.
-//
-// The Mode flag is a union of several bits named NeedName,
-// NeedFiles, and so on, each of which determines whether
-// a given field of Package (Name, Files, etc) should be
-// populated.
-//
-// For convenience, we provide named constants for the most
-// common combinations of Need flags:
-//
-// [LoadFiles] lists of files in each package
-// [LoadImports] ... plus imports
-// [LoadTypes] ... plus type information
-// [LoadSyntax] ... plus type-annotated syntax
-// [LoadAllSyntax] ... for all dependencies
-//
-// Unfortunately there are a number of open bugs related to
-// interactions among the LoadMode bits:
-// - https://github.com/golang/go/issues/56633
-// - https://github.com/golang/go/issues/56677
-// - https://github.com/golang/go/issues/58726
-// - https://github.com/golang/go/issues/63517
-type LoadMode int
-
-const (
- // NeedName adds Name and PkgPath.
- NeedName LoadMode = 1 << iota
-
- // NeedFiles adds Dir, GoFiles, OtherFiles, and IgnoredFiles
- NeedFiles
-
- // NeedCompiledGoFiles adds CompiledGoFiles.
- NeedCompiledGoFiles
-
- // NeedImports adds Imports. If NeedDeps is not set, the Imports field will contain
- // "placeholder" Packages with only the ID set.
- NeedImports
-
- // NeedDeps adds the fields requested by the LoadMode in the packages in Imports.
- NeedDeps
-
- // NeedExportFile adds ExportFile.
- NeedExportFile
-
- // NeedTypes adds Types, Fset, and IllTyped.
- NeedTypes
-
- // NeedSyntax adds Syntax and Fset.
- NeedSyntax
-
- // NeedTypesInfo adds TypesInfo and Fset.
- NeedTypesInfo
-
- // NeedTypesSizes adds TypesSizes.
- NeedTypesSizes
-
- // needInternalDepsErrors adds the internal deps errors field for use by gopls.
- needInternalDepsErrors
-
- // NeedForTest adds ForTest.
- //
- // Tests must also be set on the context for this field to be populated.
- NeedForTest
-
- // typecheckCgo enables full support for type checking cgo. Requires Go 1.15+.
- // Modifies CompiledGoFiles and Types, and has no effect on its own.
- typecheckCgo
-
- // NeedModule adds Module.
- NeedModule
-
- // NeedEmbedFiles adds EmbedFiles.
- NeedEmbedFiles
-
- // NeedEmbedPatterns adds EmbedPatterns.
- NeedEmbedPatterns
-
- // Be sure to update loadmode_string.go when adding new items!
-)
-
-const (
- // LoadFiles loads the name and file names for the initial packages.
- LoadFiles = NeedName | NeedFiles | NeedCompiledGoFiles
-
- // LoadImports loads the name, file names, and import mapping for the initial packages.
- LoadImports = LoadFiles | NeedImports
-
- // LoadTypes loads exported type information for the initial packages.
- LoadTypes = LoadImports | NeedTypes | NeedTypesSizes
-
- // LoadSyntax loads typed syntax for the initial packages.
- LoadSyntax = LoadTypes | NeedSyntax | NeedTypesInfo
-
- // LoadAllSyntax loads typed syntax for the initial packages and all dependencies.
- LoadAllSyntax = LoadSyntax | NeedDeps
-
- // Deprecated: NeedExportsFile is a historical misspelling of NeedExportFile.
- NeedExportsFile = NeedExportFile
-)
-
-// A Config specifies details about how packages should be loaded.
-// The zero value is a valid configuration.
-//
-// Calls to [Load] do not modify this struct.
-type Config struct {
- // Mode controls the level of information returned for each package.
- Mode LoadMode
-
- // Context specifies the context for the load operation.
- // Cancelling the context may cause [Load] to abort and
- // return an error.
- Context context.Context
-
- // Logf is the logger for the config.
- // If the user provides a logger, debug logging is enabled.
- // If the GOPACKAGESDEBUG environment variable is set to true,
- // but the logger is nil, default to log.Printf.
- Logf func(format string, args ...interface{})
-
- // Dir is the directory in which to run the build system's query tool
- // that provides information about the packages.
- // If Dir is empty, the tool is run in the current directory.
- Dir string
-
- // Env is the environment to use when invoking the build system's query tool.
- // If Env is nil, the current environment is used.
- // As in os/exec's Cmd, only the last value in the slice for
- // each environment key is used. To specify the setting of only
- // a few variables, append to the current environment, as in:
- //
- // opt.Env = append(os.Environ(), "GOOS=plan9", "GOARCH=386")
- //
- Env []string
-
- // BuildFlags is a list of command-line flags to be passed through to
- // the build system's query tool.
- BuildFlags []string
-
- // Fset provides source position information for syntax trees and types.
- // If Fset is nil, Load will use a new fileset, but preserve Fset's value.
- Fset *token.FileSet
-
- // ParseFile is called to read and parse each file
- // when preparing a package's type-checked syntax tree.
- // It must be safe to call ParseFile simultaneously from multiple goroutines.
- // If ParseFile is nil, the loader will uses parser.ParseFile.
- //
- // ParseFile should parse the source from src and use filename only for
- // recording position information.
- //
- // An application may supply a custom implementation of ParseFile
- // to change the effective file contents or the behavior of the parser,
- // or to modify the syntax tree. For example, selectively eliminating
- // unwanted function bodies can significantly accelerate type checking.
- ParseFile func(fset *token.FileSet, filename string, src []byte) (*ast.File, error)
-
- // If Tests is set, the loader includes not just the packages
- // matching a particular pattern but also any related test packages,
- // including test-only variants of the package and the test executable.
- //
- // For example, when using the go command, loading "fmt" with Tests=true
- // returns four packages, with IDs "fmt" (the standard package),
- // "fmt [fmt.test]" (the package as compiled for the test),
- // "fmt_test" (the test functions from source files in package fmt_test),
- // and "fmt.test" (the test binary).
- //
- // In build systems with explicit names for tests,
- // setting Tests may have no effect.
- Tests bool
-
- // Overlay is a mapping from absolute file paths to file contents.
- //
- // For each map entry, [Load] uses the alternative file
- // contents provided by the overlay mapping instead of reading
- // from the file system. This mechanism can be used to enable
- // editor-integrated tools to correctly analyze the contents
- // of modified but unsaved buffers, for example.
- //
- // The overlay mapping is passed to the build system's driver
- // (see "The driver protocol") so that it too can report
- // consistent package metadata about unsaved files. However,
- // drivers may vary in their level of support for overlays.
- Overlay map[string][]byte
-
- // -- Hidden configuration fields only for use in x/tools --
-
- // modFile will be used for -modfile in go command invocations.
- modFile string
-
- // modFlag will be used for -modfile in go command invocations.
- modFlag string
-}
-
-// Load loads and returns the Go packages named by the given patterns.
-//
-// The cfg parameter specifies loading options; nil behaves the same as an empty [Config].
-//
-// The [Config.Mode] field is a set of bits that determine what kinds
-// of information should be computed and returned. Modes that require
-// more information tend to be slower. See [LoadMode] for details
-// and important caveats. Its zero value is equivalent to
-// [NeedName] | [NeedFiles] | [NeedCompiledGoFiles].
-//
-// Each call to Load returns a new set of [Package] instances.
-// The Packages and their Imports form a directed acyclic graph.
-//
-// If the [NeedTypes] mode flag was set, each call to Load uses a new
-// [types.Importer], so [types.Object] and [types.Type] values from
-// different calls to Load must not be mixed as they will have
-// inconsistent notions of type identity.
-//
-// If any of the patterns was invalid as defined by the
-// underlying build system, Load returns an error.
-// It may return an empty list of packages without an error,
-// for instance for an empty expansion of a valid wildcard.
-// Errors associated with a particular package are recorded in the
-// corresponding Package's Errors list, and do not cause Load to
-// return an error. Clients may need to handle such errors before
-// proceeding with further analysis. The [PrintErrors] function is
-// provided for convenient display of all errors.
-func Load(cfg *Config, patterns ...string) ([]*Package, error) {
- ld := newLoader(cfg)
- response, external, err := defaultDriver(&ld.Config, patterns...)
- if err != nil {
- return nil, err
- }
-
- ld.sizes = types.SizesFor(response.Compiler, response.Arch)
- if ld.sizes == nil && ld.Config.Mode&(NeedTypes|NeedTypesSizes|NeedTypesInfo) != 0 {
- // Type size information is needed but unavailable.
- if external {
- // An external driver may fail to populate the Compiler/GOARCH fields,
- // especially since they are relatively new (see #63700).
- // Provide a sensible fallback in this case.
- ld.sizes = types.SizesFor("gc", runtime.GOARCH)
- if ld.sizes == nil { // gccgo-only arch
- ld.sizes = types.SizesFor("gc", "amd64")
- }
- } else {
- // Go list should never fail to deliver accurate size information.
- // Reject the whole Load since the error is the same for every package.
- return nil, fmt.Errorf("can't determine type sizes for compiler %q on GOARCH %q",
- response.Compiler, response.Arch)
- }
- }
-
- return ld.refine(response)
-}
-
-// defaultDriver is a driver that implements go/packages' fallback behavior.
-// It will try to request to an external driver, if one exists. If there's
-// no external driver, or the driver returns a response with NotHandled set,
-// defaultDriver will fall back to the go list driver.
-// The boolean result indicates that an external driver handled the request.
-func defaultDriver(cfg *Config, patterns ...string) (*DriverResponse, bool, error) {
- const (
- // windowsArgMax specifies the maximum command line length for
- // the Windows' CreateProcess function.
- windowsArgMax = 32767
- // maxEnvSize is a very rough estimation of the maximum environment
- // size of a user.
- maxEnvSize = 16384
- // safeArgMax specifies the maximum safe command line length to use
- // by the underlying driver excl. the environment. We choose the Windows'
- // ARG_MAX as the starting point because it's one of the lowest ARG_MAX
- // constants out of the different supported platforms,
- // e.g., https://www.in-ulm.de/~mascheck/various/argmax/#results.
- safeArgMax = windowsArgMax - maxEnvSize
- )
- chunks, err := splitIntoChunks(patterns, safeArgMax)
- if err != nil {
- return nil, false, err
- }
-
- if driver := findExternalDriver(cfg); driver != nil {
- response, err := callDriverOnChunks(driver, cfg, chunks)
- if err != nil {
- return nil, false, err
- } else if !response.NotHandled {
- return response, true, nil
- }
- // not handled: fall through
- }
-
- // go list fallback
-
- // Write overlays once, as there are many calls
- // to 'go list' (one per chunk plus others too).
- overlayFile, cleanupOverlay, err := gocommand.WriteOverlays(cfg.Overlay)
- if err != nil {
- return nil, false, err
- }
- defer cleanupOverlay()
-
- var runner gocommand.Runner // (shared across many 'go list' calls)
- driver := func(cfg *Config, patterns []string) (*DriverResponse, error) {
- return goListDriver(cfg, &runner, overlayFile, patterns)
- }
- response, err := callDriverOnChunks(driver, cfg, chunks)
- if err != nil {
- return nil, false, err
- }
- return response, false, err
-}
-
-// splitIntoChunks chunks the slice so that the total number of characters
-// in a chunk is no longer than argMax.
-func splitIntoChunks(patterns []string, argMax int) ([][]string, error) {
- if argMax <= 0 {
- return nil, errors.New("failed to split patterns into chunks, negative safe argMax value")
- }
- var chunks [][]string
- charsInChunk := 0
- nextChunkStart := 0
- for i, v := range patterns {
- vChars := len(v)
- if vChars > argMax {
- // a single pattern is longer than the maximum safe ARG_MAX, hardly should happen
- return nil, errors.New("failed to split patterns into chunks, a pattern is too long")
- }
- charsInChunk += vChars + 1 // +1 is for a whitespace between patterns that has to be counted too
- if charsInChunk > argMax {
- chunks = append(chunks, patterns[nextChunkStart:i])
- nextChunkStart = i
- charsInChunk = vChars
- }
- }
- // add the last chunk
- if nextChunkStart < len(patterns) {
- chunks = append(chunks, patterns[nextChunkStart:])
- }
- return chunks, nil
-}
-
-func callDriverOnChunks(driver driver, cfg *Config, chunks [][]string) (*DriverResponse, error) {
- if len(chunks) == 0 {
- return driver(cfg, nil)
- }
- responses := make([]*DriverResponse, len(chunks))
- errNotHandled := errors.New("driver returned NotHandled")
- var g errgroup.Group
- for i, chunk := range chunks {
- g.Go(func() (err error) {
- responses[i], err = driver(cfg, chunk)
- if responses[i] != nil && responses[i].NotHandled {
- err = errNotHandled
- }
- return err
- })
- }
- if err := g.Wait(); err != nil {
- if errors.Is(err, errNotHandled) {
- return &DriverResponse{NotHandled: true}, nil
- }
- return nil, err
- }
- return mergeResponses(responses...), nil
-}
-
-func mergeResponses(responses ...*DriverResponse) *DriverResponse {
- if len(responses) == 0 {
- return nil
- }
- response := newDeduper()
- response.dr.NotHandled = false
- response.dr.Compiler = responses[0].Compiler
- response.dr.Arch = responses[0].Arch
- response.dr.GoVersion = responses[0].GoVersion
- for _, v := range responses {
- response.addAll(v)
- }
- return response.dr
-}
-
-// A Package describes a loaded Go package.
-//
-// It also defines part of the JSON schema of [DriverResponse].
-// See the package documentation for an overview.
-type Package struct {
- // ID is a unique identifier for a package,
- // in a syntax provided by the underlying build system.
- //
- // Because the syntax varies based on the build system,
- // clients should treat IDs as opaque and not attempt to
- // interpret them.
- ID string
-
- // Name is the package name as it appears in the package source code.
- Name string
-
- // PkgPath is the package path as used by the go/types package.
- PkgPath string
-
- // Dir is the directory associated with the package, if it exists.
- //
- // For packages listed by the go command, this is the directory containing
- // the package files.
- Dir string
-
- // Errors contains any errors encountered querying the metadata
- // of the package, or while parsing or type-checking its files.
- Errors []Error
-
- // TypeErrors contains the subset of errors produced during type checking.
- TypeErrors []types.Error
-
- // GoFiles lists the absolute file paths of the package's Go source files.
- // It may include files that should not be compiled, for example because
- // they contain non-matching build tags, are documentary pseudo-files such as
- // unsafe/unsafe.go or builtin/builtin.go, or are subject to cgo preprocessing.
- GoFiles []string
-
- // CompiledGoFiles lists the absolute file paths of the package's source
- // files that are suitable for type checking.
- // This may differ from GoFiles if files are processed before compilation.
- CompiledGoFiles []string
-
- // OtherFiles lists the absolute file paths of the package's non-Go source files,
- // including assembly, C, C++, Fortran, Objective-C, SWIG, and so on.
- OtherFiles []string
-
- // EmbedFiles lists the absolute file paths of the package's files
- // embedded with go:embed.
- EmbedFiles []string
-
- // EmbedPatterns lists the absolute file patterns of the package's
- // files embedded with go:embed.
- EmbedPatterns []string
-
- // IgnoredFiles lists source files that are not part of the package
- // using the current build configuration but that might be part of
- // the package using other build configurations.
- IgnoredFiles []string
-
- // ExportFile is the absolute path to a file containing type
- // information for the package as provided by the build system.
- ExportFile string
-
- // Imports maps import paths appearing in the package's Go source files
- // to corresponding loaded Packages.
- Imports map[string]*Package
-
- // Module is the module information for the package if it exists.
- //
- // Note: it may be missing for std and cmd; see Go issue #65816.
- Module *Module
-
- // -- The following fields are not part of the driver JSON schema. --
-
- // Types provides type information for the package.
- // The NeedTypes LoadMode bit sets this field for packages matching the
- // patterns; type information for dependencies may be missing or incomplete,
- // unless NeedDeps and NeedImports are also set.
- //
- // Each call to [Load] returns a consistent set of type
- // symbols, as defined by the comment at [types.Identical].
- // Avoid mixing type information from two or more calls to [Load].
- Types *types.Package `json:"-"`
-
- // Fset provides position information for Types, TypesInfo, and Syntax.
- // It is set only when Types is set.
- Fset *token.FileSet `json:"-"`
-
- // IllTyped indicates whether the package or any dependency contains errors.
- // It is set only when Types is set.
- IllTyped bool `json:"-"`
-
- // Syntax is the package's syntax trees, for the files listed in CompiledGoFiles.
- //
- // The NeedSyntax LoadMode bit populates this field for packages matching the patterns.
- // If NeedDeps and NeedImports are also set, this field will also be populated
- // for dependencies.
- //
- // Syntax is kept in the same order as CompiledGoFiles, with the caveat that nils are
- // removed. If parsing returned nil, Syntax may be shorter than CompiledGoFiles.
- Syntax []*ast.File `json:"-"`
-
- // TypesInfo provides type information about the package's syntax trees.
- // It is set only when Syntax is set.
- TypesInfo *types.Info `json:"-"`
-
- // TypesSizes provides the effective size function for types in TypesInfo.
- TypesSizes types.Sizes `json:"-"`
-
- // -- internal --
-
- // ForTest is the package under test, if any.
- ForTest string
-
- // depsErrors is the DepsErrors field from the go list response, if any.
- depsErrors []*packagesinternal.PackageError
-}
-
-// Module provides module information for a package.
-//
-// It also defines part of the JSON schema of [DriverResponse].
-// See the package documentation for an overview.
-type Module struct {
- Path string // module path
- Version string // module version
- Replace *Module // replaced by this module
- Time *time.Time // time version was created
- Main bool // is this the main module?
- Indirect bool // is this module only an indirect dependency of main module?
- Dir string // directory holding files for this module, if any
- GoMod string // path to go.mod file used when loading this module, if any
- GoVersion string // go version used in module
- Error *ModuleError // error loading module
-}
-
-// ModuleError holds errors loading a module.
-type ModuleError struct {
- Err string // the error itself
-}
-
-func init() {
- packagesinternal.GetDepsErrors = func(p interface{}) []*packagesinternal.PackageError {
- return p.(*Package).depsErrors
- }
- packagesinternal.SetModFile = func(config interface{}, value string) {
- config.(*Config).modFile = value
- }
- packagesinternal.SetModFlag = func(config interface{}, value string) {
- config.(*Config).modFlag = value
- }
- packagesinternal.TypecheckCgo = int(typecheckCgo)
- packagesinternal.DepsErrors = int(needInternalDepsErrors)
-}
-
-// An Error describes a problem with a package's metadata, syntax, or types.
-type Error struct {
- Pos string // "file:line:col" or "file:line" or "" or "-"
- Msg string
- Kind ErrorKind
-}
-
-// ErrorKind describes the source of the error, allowing the user to
-// differentiate between errors generated by the driver, the parser, or the
-// type-checker.
-type ErrorKind int
-
-const (
- UnknownError ErrorKind = iota
- ListError
- ParseError
- TypeError
-)
-
-func (err Error) Error() string {
- pos := err.Pos
- if pos == "" {
- pos = "-" // like token.Position{}.String()
- }
- return pos + ": " + err.Msg
-}
-
-// flatPackage is the JSON form of Package
-// It drops all the type and syntax fields, and transforms the Imports
-//
-// TODO(adonovan): identify this struct with Package, effectively
-// publishing the JSON protocol.
-type flatPackage struct {
- ID string
- Name string `json:",omitempty"`
- PkgPath string `json:",omitempty"`
- Errors []Error `json:",omitempty"`
- GoFiles []string `json:",omitempty"`
- CompiledGoFiles []string `json:",omitempty"`
- OtherFiles []string `json:",omitempty"`
- EmbedFiles []string `json:",omitempty"`
- EmbedPatterns []string `json:",omitempty"`
- IgnoredFiles []string `json:",omitempty"`
- ExportFile string `json:",omitempty"`
- Imports map[string]string `json:",omitempty"`
-}
-
-// MarshalJSON returns the Package in its JSON form.
-// For the most part, the structure fields are written out unmodified, and
-// the type and syntax fields are skipped.
-// The imports are written out as just a map of path to package id.
-// The errors are written using a custom type that tries to preserve the
-// structure of error types we know about.
-//
-// This method exists to enable support for additional build systems. It is
-// not intended for use by clients of the API and we may change the format.
-func (p *Package) MarshalJSON() ([]byte, error) {
- flat := &flatPackage{
- ID: p.ID,
- Name: p.Name,
- PkgPath: p.PkgPath,
- Errors: p.Errors,
- GoFiles: p.GoFiles,
- CompiledGoFiles: p.CompiledGoFiles,
- OtherFiles: p.OtherFiles,
- EmbedFiles: p.EmbedFiles,
- EmbedPatterns: p.EmbedPatterns,
- IgnoredFiles: p.IgnoredFiles,
- ExportFile: p.ExportFile,
- }
- if len(p.Imports) > 0 {
- flat.Imports = make(map[string]string, len(p.Imports))
- for path, ipkg := range p.Imports {
- flat.Imports[path] = ipkg.ID
- }
- }
- return json.Marshal(flat)
-}
-
-// UnmarshalJSON reads in a Package from its JSON format.
-// See MarshalJSON for details about the format accepted.
-func (p *Package) UnmarshalJSON(b []byte) error {
- flat := &flatPackage{}
- if err := json.Unmarshal(b, &flat); err != nil {
- return err
- }
- *p = Package{
- ID: flat.ID,
- Name: flat.Name,
- PkgPath: flat.PkgPath,
- Errors: flat.Errors,
- GoFiles: flat.GoFiles,
- CompiledGoFiles: flat.CompiledGoFiles,
- OtherFiles: flat.OtherFiles,
- EmbedFiles: flat.EmbedFiles,
- EmbedPatterns: flat.EmbedPatterns,
- IgnoredFiles: flat.IgnoredFiles,
- ExportFile: flat.ExportFile,
- }
- if len(flat.Imports) > 0 {
- p.Imports = make(map[string]*Package, len(flat.Imports))
- for path, id := range flat.Imports {
- p.Imports[path] = &Package{ID: id}
- }
- }
- return nil
-}
-
-func (p *Package) String() string { return p.ID }
-
-// loaderPackage augments Package with state used during the loading phase
-type loaderPackage struct {
- *Package
- importErrors map[string]error // maps each bad import to its error
- preds []*loaderPackage // packages that import this one
- unfinishedSuccs atomic.Int32 // number of direct imports not yet loaded
- color uint8 // for cycle detection
- needsrc bool // load from source (Mode >= LoadTypes)
- needtypes bool // type information is either requested or depended on
- initial bool // package was matched by a pattern
- goVersion int // minor version number of go command on PATH
-}
-
-// loader holds the working state of a single call to load.
-type loader struct {
- pkgs map[string]*loaderPackage // keyed by Package.ID
- Config
- sizes types.Sizes // non-nil if needed by mode
- parseCache map[string]*parseValue
- parseCacheMu sync.Mutex
- exportMu sync.Mutex // enforces mutual exclusion of exportdata operations
-
- // Config.Mode contains the implied mode (see impliedLoadMode).
- // Implied mode contains all the fields we need the data for.
- // In requestedMode there are the actually requested fields.
- // We'll zero them out before returning packages to the user.
- // This makes it easier for us to get the conditions where
- // we need certain modes right.
- requestedMode LoadMode
-}
-
-type parseValue struct {
- f *ast.File
- err error
- ready chan struct{}
-}
-
-func newLoader(cfg *Config) *loader {
- ld := &loader{
- parseCache: map[string]*parseValue{},
- }
- if cfg != nil {
- ld.Config = *cfg
- // If the user has provided a logger, use it.
- ld.Config.Logf = cfg.Logf
- }
- if ld.Config.Logf == nil {
- // If the GOPACKAGESDEBUG environment variable is set to true,
- // but the user has not provided a logger, default to log.Printf.
- if debug {
- ld.Config.Logf = log.Printf
- } else {
- ld.Config.Logf = func(format string, args ...interface{}) {}
- }
- }
- if ld.Config.Mode == 0 {
- ld.Config.Mode = NeedName | NeedFiles | NeedCompiledGoFiles // Preserve zero behavior of Mode for backwards compatibility.
- }
- if ld.Config.Env == nil {
- ld.Config.Env = os.Environ()
- }
- if ld.Context == nil {
- ld.Context = context.Background()
- }
- if ld.Dir == "" {
- if dir, err := os.Getwd(); err == nil {
- ld.Dir = dir
- }
- }
-
- // Save the actually requested fields. We'll zero them out before returning packages to the user.
- ld.requestedMode = ld.Mode
- ld.Mode = impliedLoadMode(ld.Mode)
-
- if ld.Mode&(NeedSyntax|NeedTypes|NeedTypesInfo) != 0 {
- if ld.Fset == nil {
- ld.Fset = token.NewFileSet()
- }
-
- // ParseFile is required even in LoadTypes mode
- // because we load source if export data is missing.
- if ld.ParseFile == nil {
- ld.ParseFile = func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) {
- // We implicitly promise to keep doing ast.Object resolution. :(
- const mode = parser.AllErrors | parser.ParseComments
- return parser.ParseFile(fset, filename, src, mode)
- }
- }
- }
-
- return ld
-}
-
-// refine connects the supplied packages into a graph and then adds type
-// and syntax information as requested by the LoadMode.
-func (ld *loader) refine(response *DriverResponse) ([]*Package, error) {
- roots := response.Roots
- rootMap := make(map[string]int, len(roots))
- for i, root := range roots {
- rootMap[root] = i
- }
- ld.pkgs = make(map[string]*loaderPackage)
- // first pass, fixup and build the map and roots
- var initial = make([]*loaderPackage, len(roots))
- for _, pkg := range response.Packages {
- rootIndex := -1
- if i, found := rootMap[pkg.ID]; found {
- rootIndex = i
- }
-
- // Overlays can invalidate export data.
- // TODO(matloob): make this check fine-grained based on dependencies on overlaid files
- exportDataInvalid := len(ld.Overlay) > 0 || pkg.ExportFile == "" && pkg.PkgPath != "unsafe"
- // This package needs type information if the caller requested types and the package is
- // either a root, or it's a non-root and the user requested dependencies ...
- needtypes := (ld.Mode&(NeedTypes|NeedTypesInfo) != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0))
- // This package needs source if the call requested source (or types info, which implies source)
- // and the package is either a root, or itas a non- root and the user requested dependencies...
- needsrc := ((ld.Mode&(NeedSyntax|NeedTypesInfo) != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0)) ||
- // ... or if we need types and the exportData is invalid. We fall back to (incompletely)
- // typechecking packages from source if they fail to compile.
- (ld.Mode&(NeedTypes|NeedTypesInfo) != 0 && exportDataInvalid)) && pkg.PkgPath != "unsafe"
- lpkg := &loaderPackage{
- Package: pkg,
- needtypes: needtypes,
- needsrc: needsrc,
- goVersion: response.GoVersion,
- }
- ld.pkgs[lpkg.ID] = lpkg
- if rootIndex >= 0 {
- initial[rootIndex] = lpkg
- lpkg.initial = true
- }
- }
- for i, root := range roots {
- if initial[i] == nil {
- return nil, fmt.Errorf("root package %v is missing", root)
- }
- }
-
- // Materialize the import graph if it is needed (NeedImports),
- // or if we'll be using loadPackages (Need{Syntax|Types|TypesInfo}).
- var leaves []*loaderPackage // packages with no unfinished successors
- if ld.Mode&(NeedImports|NeedSyntax|NeedTypes|NeedTypesInfo) != 0 {
- const (
- white = 0 // new
- grey = 1 // in progress
- black = 2 // complete
- )
-
- // visit traverses the import graph, depth-first,
- // and materializes the graph as Packages.Imports.
- //
- // Valid imports are saved in the Packages.Import map.
- // Invalid imports (cycles and missing nodes) are saved in the importErrors map.
- // Thus, even in the presence of both kinds of errors,
- // the Import graph remains a DAG.
- //
- // visit returns whether the package needs src or has a transitive
- // dependency on a package that does. These are the only packages
- // for which we load source code.
- var stack []*loaderPackage
- var visit func(from, lpkg *loaderPackage) bool
- visit = func(from, lpkg *loaderPackage) bool {
- if lpkg.color == grey {
- panic("internal error: grey node")
- }
- if lpkg.color == white {
- lpkg.color = grey
- stack = append(stack, lpkg) // push
- stubs := lpkg.Imports // the structure form has only stubs with the ID in the Imports
- lpkg.Imports = make(map[string]*Package, len(stubs))
- for importPath, ipkg := range stubs {
- var importErr error
- imp := ld.pkgs[ipkg.ID]
- if imp == nil {
- // (includes package "C" when DisableCgo)
- importErr = fmt.Errorf("missing package: %q", ipkg.ID)
- } else if imp.color == grey {
- importErr = fmt.Errorf("import cycle: %s", stack)
- }
- if importErr != nil {
- if lpkg.importErrors == nil {
- lpkg.importErrors = make(map[string]error)
- }
- lpkg.importErrors[importPath] = importErr
- continue
- }
-
- if visit(lpkg, imp) {
- lpkg.needsrc = true
- }
- lpkg.Imports[importPath] = imp.Package
- }
-
- // -- postorder --
-
- // Complete type information is required for the
- // immediate dependencies of each source package.
- if lpkg.needsrc && ld.Mode&NeedTypes != 0 {
- for _, ipkg := range lpkg.Imports {
- ld.pkgs[ipkg.ID].needtypes = true
- }
- }
-
- // NeedTypeSizes causes TypeSizes to be set even
- // on packages for which types aren't needed.
- if ld.Mode&NeedTypesSizes != 0 {
- lpkg.TypesSizes = ld.sizes
- }
-
- // Add packages with no imports directly to the queue of leaves.
- if len(lpkg.Imports) == 0 {
- leaves = append(leaves, lpkg)
- }
-
- stack = stack[:len(stack)-1] // pop
- lpkg.color = black
- }
-
- // Add edge from predecessor.
- if from != nil {
- from.unfinishedSuccs.Add(+1) // incref
- lpkg.preds = append(lpkg.preds, from)
- }
-
- return lpkg.needsrc
- }
-
- // For each initial package, create its import DAG.
- for _, lpkg := range initial {
- visit(nil, lpkg)
- }
-
- } else {
- // !NeedImports: drop the stub (ID-only) import packages
- // that we are not even going to try to resolve.
- for _, lpkg := range initial {
- lpkg.Imports = nil
- }
- }
-
- // Load type data and syntax if needed, starting at
- // the initial packages (roots of the import DAG).
- if ld.Mode&(NeedSyntax|NeedTypes|NeedTypesInfo) != 0 {
-
- // We avoid using g.SetLimit to limit concurrency as
- // it makes g.Go stop accepting work, which prevents
- // workers from enqeuing, and thus finishing, and thus
- // allowing the group to make progress: deadlock.
- //
- // Instead we use the ioLimit and cpuLimit semaphores.
- g, _ := errgroup.WithContext(ld.Context)
-
- // enqueues adds a package to the type-checking queue.
- // It must have no unfinished successors.
- var enqueue func(*loaderPackage)
- enqueue = func(lpkg *loaderPackage) {
- g.Go(func() error {
- // Parse and type-check.
- ld.loadPackage(lpkg)
-
- // Notify each waiting predecessor,
- // and enqueue it when it becomes a leaf.
- for _, pred := range lpkg.preds {
- if pred.unfinishedSuccs.Add(-1) == 0 { // decref
- enqueue(pred)
- }
- }
-
- return nil
- })
- }
-
- // Load leaves first, adding new packages
- // to the queue as they become leaves.
- for _, leaf := range leaves {
- enqueue(leaf)
- }
-
- if err := g.Wait(); err != nil {
- return nil, err // cancelled
- }
- }
-
- // If the context is done, return its error and
- // throw out [likely] incomplete packages.
- if err := ld.Context.Err(); err != nil {
- return nil, err
- }
-
- result := make([]*Package, len(initial))
- for i, lpkg := range initial {
- result[i] = lpkg.Package
- }
- for i := range ld.pkgs {
- // Clear all unrequested fields,
- // to catch programs that use more than they request.
- if ld.requestedMode&NeedName == 0 {
- ld.pkgs[i].Name = ""
- ld.pkgs[i].PkgPath = ""
- }
- if ld.requestedMode&NeedFiles == 0 {
- ld.pkgs[i].GoFiles = nil
- ld.pkgs[i].OtherFiles = nil
- ld.pkgs[i].IgnoredFiles = nil
- }
- if ld.requestedMode&NeedEmbedFiles == 0 {
- ld.pkgs[i].EmbedFiles = nil
- }
- if ld.requestedMode&NeedEmbedPatterns == 0 {
- ld.pkgs[i].EmbedPatterns = nil
- }
- if ld.requestedMode&NeedCompiledGoFiles == 0 {
- ld.pkgs[i].CompiledGoFiles = nil
- }
- if ld.requestedMode&NeedImports == 0 {
- ld.pkgs[i].Imports = nil
- }
- if ld.requestedMode&NeedExportFile == 0 {
- ld.pkgs[i].ExportFile = ""
- }
- if ld.requestedMode&NeedTypes == 0 {
- ld.pkgs[i].Types = nil
- ld.pkgs[i].IllTyped = false
- }
- if ld.requestedMode&NeedSyntax == 0 {
- ld.pkgs[i].Syntax = nil
- }
- if ld.requestedMode&(NeedSyntax|NeedTypes|NeedTypesInfo) == 0 {
- ld.pkgs[i].Fset = nil
- }
- if ld.requestedMode&NeedTypesInfo == 0 {
- ld.pkgs[i].TypesInfo = nil
- }
- if ld.requestedMode&NeedTypesSizes == 0 {
- ld.pkgs[i].TypesSizes = nil
- }
- if ld.requestedMode&NeedModule == 0 {
- ld.pkgs[i].Module = nil
- }
- }
-
- return result, nil
-}
-
-// loadPackage loads/parses/typechecks the specified package.
-// It must be called only once per Package,
-// after immediate dependencies are loaded.
-// Precondition: ld.Mode&(NeedSyntax|NeedTypes|NeedTypesInfo) != 0.
-func (ld *loader) loadPackage(lpkg *loaderPackage) {
- if lpkg.PkgPath == "unsafe" {
- // Fill in the blanks to avoid surprises.
- lpkg.Types = types.Unsafe
- lpkg.Fset = ld.Fset
- lpkg.Syntax = []*ast.File{}
- lpkg.TypesInfo = new(types.Info)
- lpkg.TypesSizes = ld.sizes
- return
- }
-
- // Call NewPackage directly with explicit name.
- // This avoids skew between golist and go/types when the files'
- // package declarations are inconsistent.
- lpkg.Types = types.NewPackage(lpkg.PkgPath, lpkg.Name)
- lpkg.Fset = ld.Fset
-
- // Start shutting down if the context is done and do not load
- // source or export data files.
- // Packages that import this one will have ld.Context.Err() != nil.
- // ld.Context.Err() will be returned later by refine.
- if ld.Context.Err() != nil {
- return
- }
-
- // Subtle: we populate all Types fields with an empty Package
- // before loading export data so that export data processing
- // never has to create a types.Package for an indirect dependency,
- // which would then require that such created packages be explicitly
- // inserted back into the Import graph as a final step after export data loading.
- // (Hence this return is after the Types assignment.)
- // The Diamond test exercises this case.
- if !lpkg.needtypes && !lpkg.needsrc {
- return
- }
-
- // TODO(adonovan): this condition looks wrong:
- // I think it should be lpkg.needtypes && !lpg.needsrc,
- // so that NeedSyntax without NeedTypes can be satisfied by export data.
- if !lpkg.needsrc {
- if err := ld.loadFromExportData(lpkg); err != nil {
- lpkg.Errors = append(lpkg.Errors, Error{
- Pos: "-",
- Msg: err.Error(),
- Kind: UnknownError, // e.g. can't find/open/parse export data
- })
- }
- return // not a source package, don't get syntax trees
- }
-
- appendError := func(err error) {
- // Convert various error types into the one true Error.
- var errs []Error
- switch err := err.(type) {
- case Error:
- // from driver
- errs = append(errs, err)
-
- case *os.PathError:
- // from parser
- errs = append(errs, Error{
- Pos: err.Path + ":1",
- Msg: err.Err.Error(),
- Kind: ParseError,
- })
-
- case scanner.ErrorList:
- // from parser
- for _, err := range err {
- errs = append(errs, Error{
- Pos: err.Pos.String(),
- Msg: err.Msg,
- Kind: ParseError,
- })
- }
-
- case types.Error:
- // from type checker
- lpkg.TypeErrors = append(lpkg.TypeErrors, err)
- errs = append(errs, Error{
- Pos: err.Fset.Position(err.Pos).String(),
- Msg: err.Msg,
- Kind: TypeError,
- })
-
- default:
- // unexpected impoverished error from parser?
- errs = append(errs, Error{
- Pos: "-",
- Msg: err.Error(),
- Kind: UnknownError,
- })
-
- // If you see this error message, please file a bug.
- log.Printf("internal error: error %q (%T) without position", err, err)
- }
-
- lpkg.Errors = append(lpkg.Errors, errs...)
- }
-
- // If the go command on the PATH is newer than the runtime,
- // then the go/{scanner,ast,parser,types} packages from the
- // standard library may be unable to process the files
- // selected by go list.
- //
- // There is currently no way to downgrade the effective
- // version of the go command (see issue 52078), so we proceed
- // with the newer go command but, in case of parse or type
- // errors, we emit an additional diagnostic.
- //
- // See:
- // - golang.org/issue/52078 (flag to set release tags)
- // - golang.org/issue/50825 (gopls legacy version support)
- // - golang.org/issue/55883 (go/packages confusing error)
- //
- // Should we assert a hard minimum of (currently) go1.16 here?
- var runtimeVersion int
- if _, err := fmt.Sscanf(runtime.Version(), "go1.%d", &runtimeVersion); err == nil && runtimeVersion < lpkg.goVersion {
- defer func() {
- if len(lpkg.Errors) > 0 {
- appendError(Error{
- Pos: "-",
- Msg: fmt.Sprintf("This application uses version go1.%d of the source-processing packages but runs version go1.%d of 'go list'. It may fail to process source files that rely on newer language features. If so, rebuild the application using a newer version of Go.", runtimeVersion, lpkg.goVersion),
- Kind: UnknownError,
- })
- }
- }()
- }
-
- if ld.Config.Mode&NeedTypes != 0 && len(lpkg.CompiledGoFiles) == 0 && lpkg.ExportFile != "" {
- // The config requested loading sources and types, but sources are missing.
- // Add an error to the package and fall back to loading from export data.
- appendError(Error{"-", fmt.Sprintf("sources missing for package %s", lpkg.ID), ParseError})
- _ = ld.loadFromExportData(lpkg) // ignore any secondary errors
-
- return // can't get syntax trees for this package
- }
-
- files, errs := ld.parseFiles(lpkg.CompiledGoFiles)
- for _, err := range errs {
- appendError(err)
- }
-
- lpkg.Syntax = files
- if ld.Config.Mode&(NeedTypes|NeedTypesInfo) == 0 {
- return
- }
-
- // Start shutting down if the context is done and do not type check.
- // Packages that import this one will have ld.Context.Err() != nil.
- // ld.Context.Err() will be returned later by refine.
- if ld.Context.Err() != nil {
- return
- }
-
- // Populate TypesInfo only if needed, as it
- // causes the type checker to work much harder.
- if ld.Config.Mode&NeedTypesInfo != 0 {
- lpkg.TypesInfo = &types.Info{
- Types: make(map[ast.Expr]types.TypeAndValue),
- Defs: make(map[*ast.Ident]types.Object),
- Uses: make(map[*ast.Ident]types.Object),
- Implicits: make(map[ast.Node]types.Object),
- Instances: make(map[*ast.Ident]types.Instance),
- Scopes: make(map[ast.Node]*types.Scope),
- Selections: make(map[*ast.SelectorExpr]*types.Selection),
- FileVersions: make(map[*ast.File]string),
- }
- }
- lpkg.TypesSizes = ld.sizes
-
- importer := importerFunc(func(path string) (*types.Package, error) {
- if path == "unsafe" {
- return types.Unsafe, nil
- }
-
- // The imports map is keyed by import path.
- ipkg := lpkg.Imports[path]
- if ipkg == nil {
- if err := lpkg.importErrors[path]; err != nil {
- return nil, err
- }
- // There was skew between the metadata and the
- // import declarations, likely due to an edit
- // race, or because the ParseFile feature was
- // used to supply alternative file contents.
- return nil, fmt.Errorf("no metadata for %s", path)
- }
-
- if ipkg.Types != nil && ipkg.Types.Complete() {
- return ipkg.Types, nil
- }
- log.Fatalf("internal error: package %q without types was imported from %q", path, lpkg)
- panic("unreachable")
- })
-
- // type-check
- tc := &types.Config{
- Importer: importer,
-
- // Type-check bodies of functions only in initial packages.
- // Example: for import graph A->B->C and initial packages {A,C},
- // we can ignore function bodies in B.
- IgnoreFuncBodies: ld.Mode&NeedDeps == 0 && !lpkg.initial,
-
- Error: appendError,
- Sizes: ld.sizes, // may be nil
- }
- if lpkg.Module != nil && lpkg.Module.GoVersion != "" {
- tc.GoVersion = "go" + lpkg.Module.GoVersion
- }
- if (ld.Mode & typecheckCgo) != 0 {
- if !typesinternal.SetUsesCgo(tc) {
- appendError(Error{
- Msg: "typecheckCgo requires Go 1.15+",
- Kind: ListError,
- })
- return
- }
- }
-
- // Type-checking is CPU intensive.
- cpuLimit <- unit{} // acquire a token
- defer func() { <-cpuLimit }() // release a token
-
- typErr := types.NewChecker(tc, ld.Fset, lpkg.Types, lpkg.TypesInfo).Files(lpkg.Syntax)
- lpkg.importErrors = nil // no longer needed
-
- // In go/types go1.21 and go1.22, Checker.Files failed fast with a
- // a "too new" error, without calling tc.Error and without
- // proceeding to type-check the package (#66525).
- // We rely on the runtimeVersion error to give the suggested remedy.
- if typErr != nil && len(lpkg.Errors) == 0 && len(lpkg.Syntax) > 0 {
- if msg := typErr.Error(); strings.HasPrefix(msg, "package requires newer Go version") {
- appendError(types.Error{
- Fset: ld.Fset,
- Pos: lpkg.Syntax[0].Package,
- Msg: msg,
- })
- }
- }
-
- // If !Cgo, the type-checker uses FakeImportC mode, so
- // it doesn't invoke the importer for import "C",
- // nor report an error for the import,
- // or for any undefined C.f reference.
- // We must detect this explicitly and correctly
- // mark the package as IllTyped (by reporting an error).
- // TODO(adonovan): if these errors are annoying,
- // we could just set IllTyped quietly.
- if tc.FakeImportC {
- outer:
- for _, f := range lpkg.Syntax {
- for _, imp := range f.Imports {
- if imp.Path.Value == `"C"` {
- err := types.Error{Fset: ld.Fset, Pos: imp.Pos(), Msg: `import "C" ignored`}
- appendError(err)
- break outer
- }
- }
- }
- }
-
- // If types.Checker.Files had an error that was unreported,
- // make sure to report the unknown error so the package is illTyped.
- if typErr != nil && len(lpkg.Errors) == 0 {
- appendError(typErr)
- }
-
- // Record accumulated errors.
- illTyped := len(lpkg.Errors) > 0
- if !illTyped {
- for _, imp := range lpkg.Imports {
- if imp.IllTyped {
- illTyped = true
- break
- }
- }
- }
- lpkg.IllTyped = illTyped
-}
-
-// An importFunc is an implementation of the single-method
-// types.Importer interface based on a function value.
-type importerFunc func(path string) (*types.Package, error)
-
-func (f importerFunc) Import(path string) (*types.Package, error) { return f(path) }
-
-// We use a counting semaphore to limit
-// the number of parallel I/O calls or CPU threads per process.
-var (
- ioLimit = make(chan unit, 20)
- cpuLimit = make(chan unit, runtime.GOMAXPROCS(0))
-)
-
-func (ld *loader) parseFile(filename string) (*ast.File, error) {
- ld.parseCacheMu.Lock()
- v, ok := ld.parseCache[filename]
- if ok {
- // cache hit
- ld.parseCacheMu.Unlock()
- <-v.ready
- } else {
- // cache miss
- v = &parseValue{ready: make(chan struct{})}
- ld.parseCache[filename] = v
- ld.parseCacheMu.Unlock()
-
- var src []byte
- for f, contents := range ld.Config.Overlay {
- // TODO(adonovan): Inefficient for large overlays.
- // Do an exact name-based map lookup
- // (for nonexistent files) followed by a
- // FileID-based map lookup (for existing ones).
- if sameFile(f, filename) {
- src = contents
- break
- }
- }
- var err error
- if src == nil {
- ioLimit <- unit{} // acquire a token
- src, err = os.ReadFile(filename)
- <-ioLimit // release a token
- }
- if err != nil {
- v.err = err
- } else {
- // Parsing is CPU intensive.
- cpuLimit <- unit{} // acquire a token
- v.f, v.err = ld.ParseFile(ld.Fset, filename, src)
- <-cpuLimit // release a token
- }
-
- close(v.ready)
- }
- return v.f, v.err
-}
-
-// parseFiles reads and parses the Go source files and returns the ASTs
-// of the ones that could be at least partially parsed, along with a
-// list of I/O and parse errors encountered.
-//
-// Because files are scanned in parallel, the token.Pos
-// positions of the resulting ast.Files are not ordered.
-func (ld *loader) parseFiles(filenames []string) ([]*ast.File, []error) {
- var (
- n = len(filenames)
- parsed = make([]*ast.File, n)
- errors = make([]error, n)
- )
- var g errgroup.Group
- for i, filename := range filenames {
- // This creates goroutines unnecessarily in the
- // cache-hit case, but that case is uncommon.
- g.Go(func() error {
- parsed[i], errors[i] = ld.parseFile(filename)
- return nil
- })
- }
- g.Wait()
-
- // Eliminate nils, preserving order.
- var o int
- for _, f := range parsed {
- if f != nil {
- parsed[o] = f
- o++
- }
- }
- parsed = parsed[:o]
-
- o = 0
- for _, err := range errors {
- if err != nil {
- errors[o] = err
- o++
- }
- }
- errors = errors[:o]
-
- return parsed, errors
-}
-
-// sameFile returns true if x and y have the same basename and denote
-// the same file.
-func sameFile(x, y string) bool {
- if x == y {
- // It could be the case that y doesn't exist.
- // For instance, it may be an overlay file that
- // hasn't been written to disk. To handle that case
- // let x == y through. (We added the exact absolute path
- // string to the CompiledGoFiles list, so the unwritten
- // overlay case implies x==y.)
- return true
- }
- if strings.EqualFold(filepath.Base(x), filepath.Base(y)) { // (optimisation)
- if xi, err := os.Stat(x); err == nil {
- if yi, err := os.Stat(y); err == nil {
- return os.SameFile(xi, yi)
- }
- }
- }
- return false
-}
-
-// loadFromExportData ensures that type information is present for the specified
-// package, loading it from an export data file on the first request.
-// On success it sets lpkg.Types to a new Package.
-func (ld *loader) loadFromExportData(lpkg *loaderPackage) error {
- if lpkg.PkgPath == "" {
- log.Fatalf("internal error: Package %s has no PkgPath", lpkg)
- }
-
- // Because gcexportdata.Read has the potential to create or
- // modify the types.Package for each node in the transitive
- // closure of dependencies of lpkg, all exportdata operations
- // must be sequential. (Finer-grained locking would require
- // changes to the gcexportdata API.)
- //
- // The exportMu lock guards the lpkg.Types field and the
- // types.Package it points to, for each loaderPackage in the graph.
- //
- // Not all accesses to Package.Pkg need to be protected by exportMu:
- // graph ordering ensures that direct dependencies of source
- // packages are fully loaded before the importer reads their Pkg field.
- ld.exportMu.Lock()
- defer ld.exportMu.Unlock()
-
- if tpkg := lpkg.Types; tpkg != nil && tpkg.Complete() {
- return nil // cache hit
- }
-
- lpkg.IllTyped = true // fail safe
-
- if lpkg.ExportFile == "" {
- // Errors while building export data will have been printed to stderr.
- return fmt.Errorf("no export data file")
- }
- f, err := os.Open(lpkg.ExportFile)
- if err != nil {
- return err
- }
- defer f.Close()
-
- // Read gc export data.
- //
- // We don't currently support gccgo export data because all
- // underlying workspaces use the gc toolchain. (Even build
- // systems that support gccgo don't use it for workspace
- // queries.)
- r, err := gcexportdata.NewReader(f)
- if err != nil {
- return fmt.Errorf("reading %s: %v", lpkg.ExportFile, err)
- }
-
- // Build the view.
- //
- // The gcexportdata machinery has no concept of package ID.
- // It identifies packages by their PkgPath, which although not
- // globally unique is unique within the scope of one invocation
- // of the linker, type-checker, or gcexportdata.
- //
- // So, we must build a PkgPath-keyed view of the global
- // (conceptually ID-keyed) cache of packages and pass it to
- // gcexportdata. The view must contain every existing
- // package that might possibly be mentioned by the
- // current package---its transitive closure.
- //
- // In loadPackage, we unconditionally create a types.Package for
- // each dependency so that export data loading does not
- // create new ones.
- //
- // TODO(adonovan): it would be simpler and more efficient
- // if the export data machinery invoked a callback to
- // get-or-create a package instead of a map.
- //
- view := make(map[string]*types.Package) // view seen by gcexportdata
- seen := make(map[*loaderPackage]bool) // all visited packages
- var visit func(pkgs map[string]*Package)
- visit = func(pkgs map[string]*Package) {
- for _, p := range pkgs {
- lpkg := ld.pkgs[p.ID]
- if !seen[lpkg] {
- seen[lpkg] = true
- view[lpkg.PkgPath] = lpkg.Types
- visit(lpkg.Imports)
- }
- }
- }
- visit(lpkg.Imports)
-
- viewLen := len(view) + 1 // adding the self package
- // Parse the export data.
- // (May modify incomplete packages in view but not create new ones.)
- tpkg, err := gcexportdata.Read(r, ld.Fset, view, lpkg.PkgPath)
- if err != nil {
- return fmt.Errorf("reading %s: %v", lpkg.ExportFile, err)
- }
- if _, ok := view["go.shape"]; ok {
- // Account for the pseudopackage "go.shape" that gets
- // created by generic code.
- viewLen++
- }
- if viewLen != len(view) {
- log.Panicf("golang.org/x/tools/go/packages: unexpected new packages during load of %s", lpkg.PkgPath)
- }
-
- lpkg.Types = tpkg
- lpkg.IllTyped = false
- return nil
-}
-
-// impliedLoadMode returns loadMode with its dependencies.
-func impliedLoadMode(loadMode LoadMode) LoadMode {
- if loadMode&(NeedDeps|NeedTypes|NeedTypesInfo) != 0 {
- // All these things require knowing the import graph.
- loadMode |= NeedImports
- }
- if loadMode&NeedTypes != 0 {
- // Types require the GoVersion from Module.
- loadMode |= NeedModule
- }
-
- return loadMode
-}
-
-func usesExportData(cfg *Config) bool {
- return cfg.Mode&NeedExportFile != 0 || cfg.Mode&NeedTypes != 0 && cfg.Mode&NeedDeps == 0
-}
-
-type unit struct{}
diff --git a/vendor/golang.org/x/tools/go/packages/visit.go b/vendor/golang.org/x/tools/go/packages/visit.go
deleted file mode 100644
index df14ffd94..000000000
--- a/vendor/golang.org/x/tools/go/packages/visit.go
+++ /dev/null
@@ -1,68 +0,0 @@
-// Copyright 2018 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.
-
-package packages
-
-import (
- "fmt"
- "os"
- "sort"
-)
-
-// Visit visits all the packages in the import graph whose roots are
-// pkgs, calling the optional pre function the first time each package
-// is encountered (preorder), and the optional post function after a
-// package's dependencies have been visited (postorder).
-// The boolean result of pre(pkg) determines whether
-// the imports of package pkg are visited.
-func Visit(pkgs []*Package, pre func(*Package) bool, post func(*Package)) {
- seen := make(map[*Package]bool)
- var visit func(*Package)
- visit = func(pkg *Package) {
- if !seen[pkg] {
- seen[pkg] = true
-
- if pre == nil || pre(pkg) {
- paths := make([]string, 0, len(pkg.Imports))
- for path := range pkg.Imports {
- paths = append(paths, path)
- }
- sort.Strings(paths) // Imports is a map, this makes visit stable
- for _, path := range paths {
- visit(pkg.Imports[path])
- }
- }
-
- if post != nil {
- post(pkg)
- }
- }
- }
- for _, pkg := range pkgs {
- visit(pkg)
- }
-}
-
-// PrintErrors prints to os.Stderr the accumulated errors of all
-// packages in the import graph rooted at pkgs, dependencies first.
-// PrintErrors returns the number of errors printed.
-func PrintErrors(pkgs []*Package) int {
- var n int
- errModules := make(map[*Module]bool)
- Visit(pkgs, nil, func(pkg *Package) {
- for _, err := range pkg.Errors {
- fmt.Fprintln(os.Stderr, err)
- n++
- }
-
- // Print pkg.Module.Error once if present.
- mod := pkg.Module
- if mod != nil && mod.Error != nil && !errModules[mod] {
- errModules[mod] = true
- fmt.Fprintln(os.Stderr, mod.Error.Err)
- n++
- }
- })
- return n
-}
diff --git a/vendor/golang.org/x/tools/go/types/objectpath/objectpath.go b/vendor/golang.org/x/tools/go/types/objectpath/objectpath.go
deleted file mode 100644
index 16ed3c178..000000000
--- a/vendor/golang.org/x/tools/go/types/objectpath/objectpath.go
+++ /dev/null
@@ -1,817 +0,0 @@
-// Copyright 2018 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.
-
-// Package objectpath defines a naming scheme for types.Objects
-// (that is, named entities in Go programs) relative to their enclosing
-// package.
-//
-// Type-checker objects are canonical, so they are usually identified by
-// their address in memory (a pointer), but a pointer has meaning only
-// within one address space. By contrast, objectpath names allow the
-// identity of an object to be sent from one program to another,
-// establishing a correspondence between types.Object variables that are
-// distinct but logically equivalent.
-//
-// A single object may have multiple paths. In this example,
-//
-// type A struct{ X int }
-// type B A
-//
-// the field X has two paths due to its membership of both A and B.
-// The For(obj) function always returns one of these paths, arbitrarily
-// but consistently.
-package objectpath
-
-import (
- "fmt"
- "go/types"
- "strconv"
- "strings"
-
- "golang.org/x/tools/internal/aliases"
- "golang.org/x/tools/internal/typesinternal"
-)
-
-// TODO(adonovan): think about generic aliases.
-
-// A Path is an opaque name that identifies a types.Object
-// relative to its package. Conceptually, the name consists of a
-// sequence of destructuring operations applied to the package scope
-// to obtain the original object.
-// The name does not include the package itself.
-type Path string
-
-// Encoding
-//
-// An object path is a textual and (with training) human-readable encoding
-// of a sequence of destructuring operators, starting from a types.Package.
-// The sequences represent a path through the package/object/type graph.
-// We classify these operators by their type:
-//
-// PO package->object Package.Scope.Lookup
-// OT object->type Object.Type
-// TT type->type Type.{Elem,Key,{,{,Recv}Type}Params,Results,Underlying,Rhs} [EKPRUTrCa]
-// TO type->object Type.{At,Field,Method,Obj} [AFMO]
-//
-// All valid paths start with a package and end at an object
-// and thus may be defined by the regular language:
-//
-// objectpath = PO (OT TT* TO)*
-//
-// The concrete encoding follows directly:
-// - The only PO operator is Package.Scope.Lookup, which requires an identifier.
-// - The only OT operator is Object.Type,
-// which we encode as '.' because dot cannot appear in an identifier.
-// - The TT operators are encoded as [EKPRUTrCa];
-// two of these ({,Recv}TypeParams) require an integer operand,
-// which is encoded as a string of decimal digits.
-// - The TO operators are encoded as [AFMO];
-// three of these (At,Field,Method) require an integer operand,
-// which is encoded as a string of decimal digits.
-// These indices are stable across different representations
-// of the same package, even source and export data.
-// The indices used are implementation specific and may not correspond to
-// the argument to the go/types function.
-//
-// In the example below,
-//
-// package p
-//
-// type T interface {
-// f() (a string, b struct{ X int })
-// }
-//
-// field X has the path "T.UM0.RA1.F0",
-// representing the following sequence of operations:
-//
-// p.Lookup("T") T
-// .Type().Underlying().Method(0). f
-// .Type().Results().At(1) b
-// .Type().Field(0) X
-//
-// The encoding is not maximally compact---every R or P is
-// followed by an A, for example---but this simplifies the
-// encoder and decoder.
-const (
- // object->type operators
- opType = '.' // .Type() (Object)
-
- // type->type operators
- opElem = 'E' // .Elem() (Pointer, Slice, Array, Chan, Map)
- opKey = 'K' // .Key() (Map)
- opParams = 'P' // .Params() (Signature)
- opResults = 'R' // .Results() (Signature)
- opUnderlying = 'U' // .Underlying() (Named)
- opTypeParam = 'T' // .TypeParams.At(i) (Named, Signature)
- opRecvTypeParam = 'r' // .RecvTypeParams.At(i) (Signature)
- opConstraint = 'C' // .Constraint() (TypeParam)
- opRhs = 'a' // .Rhs() (Alias)
-
- // type->object operators
- opAt = 'A' // .At(i) (Tuple)
- opField = 'F' // .Field(i) (Struct)
- opMethod = 'M' // .Method(i) (Named or Interface; not Struct: "promoted" names are ignored)
- opObj = 'O' // .Obj() (Named, TypeParam)
-)
-
-// For is equivalent to new(Encoder).For(obj).
-//
-// It may be more efficient to reuse a single Encoder across several calls.
-func For(obj types.Object) (Path, error) {
- return new(Encoder).For(obj)
-}
-
-// An Encoder amortizes the cost of encoding the paths of multiple objects.
-// The zero value of an Encoder is ready to use.
-type Encoder struct {
- scopeMemo map[*types.Scope][]types.Object // memoization of scopeObjects
-}
-
-// For returns the path to an object relative to its package,
-// or an error if the object is not accessible from the package's Scope.
-//
-// The For function guarantees to return a path only for the following objects:
-// - package-level types
-// - exported package-level non-types
-// - methods
-// - parameter and result variables
-// - struct fields
-// These objects are sufficient to define the API of their package.
-// The objects described by a package's export data are drawn from this set.
-//
-// The set of objects accessible from a package's Scope depends on
-// whether the package was produced by type-checking syntax, or
-// reading export data; the latter may have a smaller Scope since
-// export data trims objects that are not reachable from an exported
-// declaration. For example, the For function will return a path for
-// an exported method of an unexported type that is not reachable
-// from any public declaration; this path will cause the Object
-// function to fail if called on a package loaded from export data.
-// TODO(adonovan): is this a bug or feature? Should this package
-// compute accessibility in the same way?
-//
-// For does not return a path for predeclared names, imported package
-// names, local names, and unexported package-level names (except
-// types).
-//
-// Example: given this definition,
-//
-// package p
-//
-// type T interface {
-// f() (a string, b struct{ X int })
-// }
-//
-// For(X) would return a path that denotes the following sequence of operations:
-//
-// p.Scope().Lookup("T") (TypeName T)
-// .Type().Underlying().Method(0). (method Func f)
-// .Type().Results().At(1) (field Var b)
-// .Type().Field(0) (field Var X)
-//
-// where p is the package (*types.Package) to which X belongs.
-func (enc *Encoder) For(obj types.Object) (Path, error) {
- pkg := obj.Pkg()
-
- // This table lists the cases of interest.
- //
- // Object Action
- // ------ ------
- // nil reject
- // builtin reject
- // pkgname reject
- // label reject
- // var
- // package-level accept
- // func param/result accept
- // local reject
- // struct field accept
- // const
- // package-level accept
- // local reject
- // func
- // package-level accept
- // init functions reject
- // concrete method accept
- // interface method accept
- // type
- // package-level accept
- // local reject
- //
- // The only accessible package-level objects are members of pkg itself.
- //
- // The cases are handled in four steps:
- //
- // 1. reject nil and builtin
- // 2. accept package-level objects
- // 3. reject obviously invalid objects
- // 4. search the API for the path to the param/result/field/method.
-
- // 1. reference to nil or builtin?
- if pkg == nil {
- return "", fmt.Errorf("predeclared %s has no path", obj)
- }
- scope := pkg.Scope()
-
- // 2. package-level object?
- if scope.Lookup(obj.Name()) == obj {
- // Only exported objects (and non-exported types) have a path.
- // Non-exported types may be referenced by other objects.
- if _, ok := obj.(*types.TypeName); !ok && !obj.Exported() {
- return "", fmt.Errorf("no path for non-exported %v", obj)
- }
- return Path(obj.Name()), nil
- }
-
- // 3. Not a package-level object.
- // Reject obviously non-viable cases.
- switch obj := obj.(type) {
- case *types.TypeName:
- if _, ok := types.Unalias(obj.Type()).(*types.TypeParam); !ok {
- // With the exception of type parameters, only package-level type names
- // have a path.
- return "", fmt.Errorf("no path for %v", obj)
- }
- case *types.Const, // Only package-level constants have a path.
- *types.Label, // Labels are function-local.
- *types.PkgName: // PkgNames are file-local.
- return "", fmt.Errorf("no path for %v", obj)
-
- case *types.Var:
- // Could be:
- // - a field (obj.IsField())
- // - a func parameter or result
- // - a local var.
- // Sadly there is no way to distinguish
- // a param/result from a local
- // so we must proceed to the find.
-
- case *types.Func:
- // A func, if not package-level, must be a method.
- if recv := obj.Type().(*types.Signature).Recv(); recv == nil {
- return "", fmt.Errorf("func is not a method: %v", obj)
- }
-
- if path, ok := enc.concreteMethod(obj); ok {
- // Fast path for concrete methods that avoids looping over scope.
- return path, nil
- }
-
- default:
- panic(obj)
- }
-
- // 4. Search the API for the path to the var (field/param/result) or method.
-
- // First inspect package-level named types.
- // In the presence of path aliases, these give
- // the best paths because non-types may
- // refer to types, but not the reverse.
- empty := make([]byte, 0, 48) // initial space
- objs := enc.scopeObjects(scope)
- for _, o := range objs {
- tname, ok := o.(*types.TypeName)
- if !ok {
- continue // handle non-types in second pass
- }
-
- path := append(empty, o.Name()...)
- path = append(path, opType)
-
- T := o.Type()
- if alias, ok := T.(*types.Alias); ok {
- if r := findTypeParam(obj, aliases.TypeParams(alias), path, opTypeParam); r != nil {
- return Path(r), nil
- }
- if r := find(obj, aliases.Rhs(alias), append(path, opRhs)); r != nil {
- return Path(r), nil
- }
-
- } else if tname.IsAlias() {
- // legacy alias
- if r := find(obj, T, path); r != nil {
- return Path(r), nil
- }
-
- } else if named, ok := T.(*types.Named); ok {
- // defined (named) type
- if r := findTypeParam(obj, named.TypeParams(), path, opTypeParam); r != nil {
- return Path(r), nil
- }
- if r := find(obj, named.Underlying(), append(path, opUnderlying)); r != nil {
- return Path(r), nil
- }
- }
- }
-
- // Then inspect everything else:
- // non-types, and declared methods of defined types.
- for _, o := range objs {
- path := append(empty, o.Name()...)
- if _, ok := o.(*types.TypeName); !ok {
- if o.Exported() {
- // exported non-type (const, var, func)
- if r := find(obj, o.Type(), append(path, opType)); r != nil {
- return Path(r), nil
- }
- }
- continue
- }
-
- // Inspect declared methods of defined types.
- if T, ok := types.Unalias(o.Type()).(*types.Named); ok {
- path = append(path, opType)
- // The method index here is always with respect
- // to the underlying go/types data structures,
- // which ultimately derives from source order
- // and must be preserved by export data.
- for i := 0; i < T.NumMethods(); i++ {
- m := T.Method(i)
- path2 := appendOpArg(path, opMethod, i)
- if m == obj {
- return Path(path2), nil // found declared method
- }
- if r := find(obj, m.Type(), append(path2, opType)); r != nil {
- return Path(r), nil
- }
- }
- }
- }
-
- return "", fmt.Errorf("can't find path for %v in %s", obj, pkg.Path())
-}
-
-func appendOpArg(path []byte, op byte, arg int) []byte {
- path = append(path, op)
- path = strconv.AppendInt(path, int64(arg), 10)
- return path
-}
-
-// concreteMethod returns the path for meth, which must have a non-nil receiver.
-// The second return value indicates success and may be false if the method is
-// an interface method or if it is an instantiated method.
-//
-// This function is just an optimization that avoids the general scope walking
-// approach. You are expected to fall back to the general approach if this
-// function fails.
-func (enc *Encoder) concreteMethod(meth *types.Func) (Path, bool) {
- // Concrete methods can only be declared on package-scoped named types. For
- // that reason we can skip the expensive walk over the package scope: the
- // path will always be package -> named type -> method. We can trivially get
- // the type name from the receiver, and only have to look over the type's
- // methods to find the method index.
- //
- // Methods on generic types require special consideration, however. Consider
- // the following package:
- //
- // L1: type S[T any] struct{}
- // L2: func (recv S[A]) Foo() { recv.Bar() }
- // L3: func (recv S[B]) Bar() { }
- // L4: type Alias = S[int]
- // L5: func _[T any]() { var s S[int]; s.Foo() }
- //
- // The receivers of methods on generic types are instantiations. L2 and L3
- // instantiate S with the type-parameters A and B, which are scoped to the
- // respective methods. L4 and L5 each instantiate S with int. Each of these
- // instantiations has its own method set, full of methods (and thus objects)
- // with receivers whose types are the respective instantiations. In other
- // words, we have
- //
- // S[A].Foo, S[A].Bar
- // S[B].Foo, S[B].Bar
- // S[int].Foo, S[int].Bar
- //
- // We may thus be trying to produce object paths for any of these objects.
- //
- // S[A].Foo and S[B].Bar are the origin methods, and their paths are S.Foo
- // and S.Bar, which are the paths that this function naturally produces.
- //
- // S[A].Bar, S[B].Foo, and both methods on S[int] are instantiations that
- // don't correspond to the origin methods. For S[int], this is significant.
- // The most precise object path for S[int].Foo, for example, is Alias.Foo,
- // not S.Foo. Our function, however, would produce S.Foo, which would
- // resolve to a different object.
- //
- // For S[A].Bar and S[B].Foo it could be argued that S.Bar and S.Foo are
- // still the correct paths, since only the origin methods have meaningful
- // paths. But this is likely only true for trivial cases and has edge cases.
- // Since this function is only an optimization, we err on the side of giving
- // up, deferring to the slower but definitely correct algorithm. Most users
- // of objectpath will only be giving us origin methods, anyway, as referring
- // to instantiated methods is usually not useful.
-
- if meth.Origin() != meth {
- return "", false
- }
-
- _, named := typesinternal.ReceiverNamed(meth.Type().(*types.Signature).Recv())
- if named == nil {
- return "", false
- }
-
- if types.IsInterface(named) {
- // Named interfaces don't have to be package-scoped
- //
- // TODO(dominikh): opt: if scope.Lookup(name) == named, then we can apply this optimization to interface
- // methods, too, I think.
- return "", false
- }
-
- // Preallocate space for the name, opType, opMethod, and some digits.
- name := named.Obj().Name()
- path := make([]byte, 0, len(name)+8)
- path = append(path, name...)
- path = append(path, opType)
-
- // Method indices are w.r.t. the go/types data structures,
- // ultimately deriving from source order,
- // which is preserved by export data.
- for i := 0; i < named.NumMethods(); i++ {
- if named.Method(i) == meth {
- path = appendOpArg(path, opMethod, i)
- return Path(path), true
- }
- }
-
- // Due to golang/go#59944, go/types fails to associate the receiver with
- // certain methods on cgo types.
- //
- // TODO(rfindley): replace this panic once golang/go#59944 is fixed in all Go
- // versions gopls supports.
- return "", false
- // panic(fmt.Sprintf("couldn't find method %s on type %s; methods: %#v", meth, named, enc.namedMethods(named)))
-}
-
-// find finds obj within type T, returning the path to it, or nil if not found.
-//
-// The seen map is used to short circuit cycles through type parameters. If
-// nil, it will be allocated as necessary.
-//
-// The seenMethods map is used internally to short circuit cycles through
-// interface methods, such as occur in the following example:
-//
-// type I interface { f() interface{I} }
-//
-// See golang/go#68046 for details.
-func find(obj types.Object, T types.Type, path []byte) []byte {
- return (&finder{obj: obj}).find(T, path)
-}
-
-// finder closes over search state for a call to find.
-type finder struct {
- obj types.Object // the sought object
- seenTParamNames map[*types.TypeName]bool // for cycle breaking through type parameters
- seenMethods map[*types.Func]bool // for cycle breaking through recursive interfaces
-}
-
-func (f *finder) find(T types.Type, path []byte) []byte {
- switch T := T.(type) {
- case *types.Alias:
- return f.find(types.Unalias(T), path)
- case *types.Basic, *types.Named:
- // Named types belonging to pkg were handled already,
- // so T must belong to another package. No path.
- return nil
- case *types.Pointer:
- return f.find(T.Elem(), append(path, opElem))
- case *types.Slice:
- return f.find(T.Elem(), append(path, opElem))
- case *types.Array:
- return f.find(T.Elem(), append(path, opElem))
- case *types.Chan:
- return f.find(T.Elem(), append(path, opElem))
- case *types.Map:
- if r := f.find(T.Key(), append(path, opKey)); r != nil {
- return r
- }
- return f.find(T.Elem(), append(path, opElem))
- case *types.Signature:
- if r := f.findTypeParam(T.RecvTypeParams(), path, opRecvTypeParam); r != nil {
- return r
- }
- if r := f.findTypeParam(T.TypeParams(), path, opTypeParam); r != nil {
- return r
- }
- if r := f.find(T.Params(), append(path, opParams)); r != nil {
- return r
- }
- return f.find(T.Results(), append(path, opResults))
- case *types.Struct:
- for i := 0; i < T.NumFields(); i++ {
- fld := T.Field(i)
- path2 := appendOpArg(path, opField, i)
- if fld == f.obj {
- return path2 // found field var
- }
- if r := f.find(fld.Type(), append(path2, opType)); r != nil {
- return r
- }
- }
- return nil
- case *types.Tuple:
- for i := 0; i < T.Len(); i++ {
- v := T.At(i)
- path2 := appendOpArg(path, opAt, i)
- if v == f.obj {
- return path2 // found param/result var
- }
- if r := f.find(v.Type(), append(path2, opType)); r != nil {
- return r
- }
- }
- return nil
- case *types.Interface:
- for i := 0; i < T.NumMethods(); i++ {
- m := T.Method(i)
- if f.seenMethods[m] {
- return nil
- }
- path2 := appendOpArg(path, opMethod, i)
- if m == f.obj {
- return path2 // found interface method
- }
- if f.seenMethods == nil {
- f.seenMethods = make(map[*types.Func]bool)
- }
- f.seenMethods[m] = true
- if r := f.find(m.Type(), append(path2, opType)); r != nil {
- return r
- }
- }
- return nil
- case *types.TypeParam:
- name := T.Obj()
- if f.seenTParamNames[name] {
- return nil
- }
- if name == f.obj {
- return append(path, opObj)
- }
- if f.seenTParamNames == nil {
- f.seenTParamNames = make(map[*types.TypeName]bool)
- }
- f.seenTParamNames[name] = true
- if r := f.find(T.Constraint(), append(path, opConstraint)); r != nil {
- return r
- }
- return nil
- }
- panic(T)
-}
-
-func findTypeParam(obj types.Object, list *types.TypeParamList, path []byte, op byte) []byte {
- return (&finder{obj: obj}).findTypeParam(list, path, op)
-}
-
-func (f *finder) findTypeParam(list *types.TypeParamList, path []byte, op byte) []byte {
- for i := 0; i < list.Len(); i++ {
- tparam := list.At(i)
- path2 := appendOpArg(path, op, i)
- if r := f.find(tparam, path2); r != nil {
- return r
- }
- }
- return nil
-}
-
-// Object returns the object denoted by path p within the package pkg.
-func Object(pkg *types.Package, p Path) (types.Object, error) {
- pathstr := string(p)
- if pathstr == "" {
- return nil, fmt.Errorf("empty path")
- }
-
- var pkgobj, suffix string
- if dot := strings.IndexByte(pathstr, opType); dot < 0 {
- pkgobj = pathstr
- } else {
- pkgobj = pathstr[:dot]
- suffix = pathstr[dot:] // suffix starts with "."
- }
-
- obj := pkg.Scope().Lookup(pkgobj)
- if obj == nil {
- return nil, fmt.Errorf("package %s does not contain %q", pkg.Path(), pkgobj)
- }
-
- // abstraction of *types.{Pointer,Slice,Array,Chan,Map}
- type hasElem interface {
- Elem() types.Type
- }
- // abstraction of *types.{Named,Signature}
- type hasTypeParams interface {
- TypeParams() *types.TypeParamList
- }
- // abstraction of *types.{Named,TypeParam}
- type hasObj interface {
- Obj() *types.TypeName
- }
-
- // The loop state is the pair (t, obj),
- // exactly one of which is non-nil, initially obj.
- // All suffixes start with '.' (the only object->type operation),
- // followed by optional type->type operations,
- // then a type->object operation.
- // The cycle then repeats.
- var t types.Type
- for suffix != "" {
- code := suffix[0]
- suffix = suffix[1:]
-
- // Codes [AFMTr] have an integer operand.
- var index int
- switch code {
- case opAt, opField, opMethod, opTypeParam, opRecvTypeParam:
- rest := strings.TrimLeft(suffix, "0123456789")
- numerals := suffix[:len(suffix)-len(rest)]
- suffix = rest
- i, err := strconv.Atoi(numerals)
- if err != nil {
- return nil, fmt.Errorf("invalid path: bad numeric operand %q for code %q", numerals, code)
- }
- index = int(i)
- case opObj:
- // no operand
- default:
- // The suffix must end with a type->object operation.
- if suffix == "" {
- return nil, fmt.Errorf("invalid path: ends with %q, want [AFMO]", code)
- }
- }
-
- if code == opType {
- if t != nil {
- return nil, fmt.Errorf("invalid path: unexpected %q in type context", opType)
- }
- t = obj.Type()
- obj = nil
- continue
- }
-
- if t == nil {
- return nil, fmt.Errorf("invalid path: code %q in object context", code)
- }
-
- // Inv: t != nil, obj == nil
-
- t = types.Unalias(t)
- switch code {
- case opElem:
- hasElem, ok := t.(hasElem) // Pointer, Slice, Array, Chan, Map
- if !ok {
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want pointer, slice, array, chan or map)", code, t, t)
- }
- t = hasElem.Elem()
-
- case opKey:
- mapType, ok := t.(*types.Map)
- if !ok {
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want map)", code, t, t)
- }
- t = mapType.Key()
-
- case opParams:
- sig, ok := t.(*types.Signature)
- if !ok {
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want signature)", code, t, t)
- }
- t = sig.Params()
-
- case opResults:
- sig, ok := t.(*types.Signature)
- if !ok {
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want signature)", code, t, t)
- }
- t = sig.Results()
-
- case opUnderlying:
- named, ok := t.(*types.Named)
- if !ok {
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want named)", code, t, t)
- }
- t = named.Underlying()
-
- case opRhs:
- if alias, ok := t.(*types.Alias); ok {
- t = aliases.Rhs(alias)
- } else if false && aliases.Enabled() {
- // The Enabled check is too expensive, so for now we
- // simply assume that aliases are not enabled.
- // TODO(adonovan): replace with "if true {" when go1.24 is assured.
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want alias)", code, t, t)
- }
-
- case opTypeParam:
- hasTypeParams, ok := t.(hasTypeParams) // Named, Signature
- if !ok {
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want named or signature)", code, t, t)
- }
- tparams := hasTypeParams.TypeParams()
- if n := tparams.Len(); index >= n {
- return nil, fmt.Errorf("tuple index %d out of range [0-%d)", index, n)
- }
- t = tparams.At(index)
-
- case opRecvTypeParam:
- sig, ok := t.(*types.Signature) // Signature
- if !ok {
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want signature)", code, t, t)
- }
- rtparams := sig.RecvTypeParams()
- if n := rtparams.Len(); index >= n {
- return nil, fmt.Errorf("tuple index %d out of range [0-%d)", index, n)
- }
- t = rtparams.At(index)
-
- case opConstraint:
- tparam, ok := t.(*types.TypeParam)
- if !ok {
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want type parameter)", code, t, t)
- }
- t = tparam.Constraint()
-
- case opAt:
- tuple, ok := t.(*types.Tuple)
- if !ok {
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want tuple)", code, t, t)
- }
- if n := tuple.Len(); index >= n {
- return nil, fmt.Errorf("tuple index %d out of range [0-%d)", index, n)
- }
- obj = tuple.At(index)
- t = nil
-
- case opField:
- structType, ok := t.(*types.Struct)
- if !ok {
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want struct)", code, t, t)
- }
- if n := structType.NumFields(); index >= n {
- return nil, fmt.Errorf("field index %d out of range [0-%d)", index, n)
- }
- obj = structType.Field(index)
- t = nil
-
- case opMethod:
- switch t := t.(type) {
- case *types.Interface:
- if index >= t.NumMethods() {
- return nil, fmt.Errorf("method index %d out of range [0-%d)", index, t.NumMethods())
- }
- obj = t.Method(index) // Id-ordered
-
- case *types.Named:
- if index >= t.NumMethods() {
- return nil, fmt.Errorf("method index %d out of range [0-%d)", index, t.NumMethods())
- }
- obj = t.Method(index)
-
- default:
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want interface or named)", code, t, t)
- }
- t = nil
-
- case opObj:
- hasObj, ok := t.(hasObj)
- if !ok {
- return nil, fmt.Errorf("cannot apply %q to %s (got %T, want named or type param)", code, t, t)
- }
- obj = hasObj.Obj()
- t = nil
-
- default:
- return nil, fmt.Errorf("invalid path: unknown code %q", code)
- }
- }
-
- if obj == nil {
- panic(p) // path does not end in an object-valued operator
- }
-
- if obj.Pkg() != pkg {
- return nil, fmt.Errorf("path denotes %s, which belongs to a different package", obj)
- }
-
- return obj, nil // success
-}
-
-// scopeObjects is a memoization of scope objects.
-// Callers must not modify the result.
-func (enc *Encoder) scopeObjects(scope *types.Scope) []types.Object {
- m := enc.scopeMemo
- if m == nil {
- m = make(map[*types.Scope][]types.Object)
- enc.scopeMemo = m
- }
- objs, ok := m[scope]
- if !ok {
- names := scope.Names() // allocates and sorts
- objs = make([]types.Object, len(names))
- for i, name := range names {
- objs[i] = scope.Lookup(name)
- }
- m[scope] = objs
- }
- return objs
-}
diff --git a/vendor/golang.org/x/tools/go/types/typeutil/callee.go b/vendor/golang.org/x/tools/go/types/typeutil/callee.go
deleted file mode 100644
index 754380351..000000000
--- a/vendor/golang.org/x/tools/go/types/typeutil/callee.go
+++ /dev/null
@@ -1,68 +0,0 @@
-// Copyright 2018 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.
-
-package typeutil
-
-import (
- "go/ast"
- "go/types"
-
- "golang.org/x/tools/internal/typeparams"
-)
-
-// Callee returns the named target of a function call, if any:
-// a function, method, builtin, or variable.
-//
-// Functions and methods may potentially have type parameters.
-func Callee(info *types.Info, call *ast.CallExpr) types.Object {
- fun := ast.Unparen(call.Fun)
-
- // Look through type instantiation if necessary.
- isInstance := false
- switch fun.(type) {
- case *ast.IndexExpr, *ast.IndexListExpr:
- // When extracting the callee from an *IndexExpr, we need to check that
- // it is a *types.Func and not a *types.Var.
- // Example: Don't match a slice m within the expression `m[0]()`.
- isInstance = true
- fun, _, _, _ = typeparams.UnpackIndexExpr(fun)
- }
-
- var obj types.Object
- switch fun := fun.(type) {
- case *ast.Ident:
- obj = info.Uses[fun] // type, var, builtin, or declared func
- case *ast.SelectorExpr:
- if sel, ok := info.Selections[fun]; ok {
- obj = sel.Obj() // method or field
- } else {
- obj = info.Uses[fun.Sel] // qualified identifier?
- }
- }
- if _, ok := obj.(*types.TypeName); ok {
- return nil // T(x) is a conversion, not a call
- }
- // A Func is required to match instantiations.
- if _, ok := obj.(*types.Func); isInstance && !ok {
- return nil // Was not a Func.
- }
- return obj
-}
-
-// StaticCallee returns the target (function or method) of a static function
-// call, if any. It returns nil for calls to builtins.
-//
-// Note: for calls of instantiated functions and methods, StaticCallee returns
-// the corresponding generic function or method on the generic type.
-func StaticCallee(info *types.Info, call *ast.CallExpr) *types.Func {
- if f, ok := Callee(info, call).(*types.Func); ok && !interfaceMethod(f) {
- return f
- }
- return nil
-}
-
-func interfaceMethod(f *types.Func) bool {
- recv := f.Type().(*types.Signature).Recv()
- return recv != nil && types.IsInterface(recv.Type())
-}
diff --git a/vendor/golang.org/x/tools/go/types/typeutil/imports.go b/vendor/golang.org/x/tools/go/types/typeutil/imports.go
deleted file mode 100644
index b81ce0c33..000000000
--- a/vendor/golang.org/x/tools/go/types/typeutil/imports.go
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright 2014 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.
-
-package typeutil
-
-import "go/types"
-
-// Dependencies returns all dependencies of the specified packages.
-//
-// Dependent packages appear in topological order: if package P imports
-// package Q, Q appears earlier than P in the result.
-// The algorithm follows import statements in the order they
-// appear in the source code, so the result is a total order.
-func Dependencies(pkgs ...*types.Package) []*types.Package {
- var result []*types.Package
- seen := make(map[*types.Package]bool)
- var visit func(pkgs []*types.Package)
- visit = func(pkgs []*types.Package) {
- for _, p := range pkgs {
- if !seen[p] {
- seen[p] = true
- visit(p.Imports())
- result = append(result, p)
- }
- }
- }
- visit(pkgs)
- return result
-}
diff --git a/vendor/golang.org/x/tools/go/types/typeutil/map.go b/vendor/golang.org/x/tools/go/types/typeutil/map.go
deleted file mode 100644
index 8d824f714..000000000
--- a/vendor/golang.org/x/tools/go/types/typeutil/map.go
+++ /dev/null
@@ -1,517 +0,0 @@
-// Copyright 2014 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.
-
-// Package typeutil defines various utilities for types, such as Map,
-// a mapping from types.Type to any values.
-package typeutil // import "golang.org/x/tools/go/types/typeutil"
-
-import (
- "bytes"
- "fmt"
- "go/types"
- "reflect"
-
- "golang.org/x/tools/internal/typeparams"
-)
-
-// Map is a hash-table-based mapping from types (types.Type) to
-// arbitrary any values. The concrete types that implement
-// the Type interface are pointers. Since they are not canonicalized,
-// == cannot be used to check for equivalence, and thus we cannot
-// simply use a Go map.
-//
-// Just as with map[K]V, a nil *Map is a valid empty map.
-//
-// Not thread-safe.
-type Map struct {
- hasher Hasher // shared by many Maps
- table map[uint32][]entry // maps hash to bucket; entry.key==nil means unused
- length int // number of map entries
-}
-
-// entry is an entry (key/value association) in a hash bucket.
-type entry struct {
- key types.Type
- value any
-}
-
-// SetHasher sets the hasher used by Map.
-//
-// All Hashers are functionally equivalent but contain internal state
-// used to cache the results of hashing previously seen types.
-//
-// A single Hasher created by MakeHasher() may be shared among many
-// Maps. This is recommended if the instances have many keys in
-// common, as it will amortize the cost of hash computation.
-//
-// A Hasher may grow without bound as new types are seen. Even when a
-// type is deleted from the map, the Hasher never shrinks, since other
-// types in the map may reference the deleted type indirectly.
-//
-// Hashers are not thread-safe, and read-only operations such as
-// Map.Lookup require updates to the hasher, so a full Mutex lock (not a
-// read-lock) is require around all Map operations if a shared
-// hasher is accessed from multiple threads.
-//
-// If SetHasher is not called, the Map will create a private hasher at
-// the first call to Insert.
-func (m *Map) SetHasher(hasher Hasher) {
- m.hasher = hasher
-}
-
-// Delete removes the entry with the given key, if any.
-// It returns true if the entry was found.
-func (m *Map) Delete(key types.Type) bool {
- if m != nil && m.table != nil {
- hash := m.hasher.Hash(key)
- bucket := m.table[hash]
- for i, e := range bucket {
- if e.key != nil && types.Identical(key, e.key) {
- // We can't compact the bucket as it
- // would disturb iterators.
- bucket[i] = entry{}
- m.length--
- return true
- }
- }
- }
- return false
-}
-
-// At returns the map entry for the given key.
-// The result is nil if the entry is not present.
-func (m *Map) At(key types.Type) any {
- if m != nil && m.table != nil {
- for _, e := range m.table[m.hasher.Hash(key)] {
- if e.key != nil && types.Identical(key, e.key) {
- return e.value
- }
- }
- }
- return nil
-}
-
-// Set sets the map entry for key to val,
-// and returns the previous entry, if any.
-func (m *Map) Set(key types.Type, value any) (prev any) {
- if m.table != nil {
- hash := m.hasher.Hash(key)
- bucket := m.table[hash]
- var hole *entry
- for i, e := range bucket {
- if e.key == nil {
- hole = &bucket[i]
- } else if types.Identical(key, e.key) {
- prev = e.value
- bucket[i].value = value
- return
- }
- }
-
- if hole != nil {
- *hole = entry{key, value} // overwrite deleted entry
- } else {
- m.table[hash] = append(bucket, entry{key, value})
- }
- } else {
- if m.hasher.memo == nil {
- m.hasher = MakeHasher()
- }
- hash := m.hasher.Hash(key)
- m.table = map[uint32][]entry{hash: {entry{key, value}}}
- }
-
- m.length++
- return
-}
-
-// Len returns the number of map entries.
-func (m *Map) Len() int {
- if m != nil {
- return m.length
- }
- return 0
-}
-
-// Iterate calls function f on each entry in the map in unspecified order.
-//
-// If f should mutate the map, Iterate provides the same guarantees as
-// Go maps: if f deletes a map entry that Iterate has not yet reached,
-// f will not be invoked for it, but if f inserts a map entry that
-// Iterate has not yet reached, whether or not f will be invoked for
-// it is unspecified.
-func (m *Map) Iterate(f func(key types.Type, value any)) {
- if m != nil {
- for _, bucket := range m.table {
- for _, e := range bucket {
- if e.key != nil {
- f(e.key, e.value)
- }
- }
- }
- }
-}
-
-// Keys returns a new slice containing the set of map keys.
-// The order is unspecified.
-func (m *Map) Keys() []types.Type {
- keys := make([]types.Type, 0, m.Len())
- m.Iterate(func(key types.Type, _ any) {
- keys = append(keys, key)
- })
- return keys
-}
-
-func (m *Map) toString(values bool) string {
- if m == nil {
- return "{}"
- }
- var buf bytes.Buffer
- fmt.Fprint(&buf, "{")
- sep := ""
- m.Iterate(func(key types.Type, value any) {
- fmt.Fprint(&buf, sep)
- sep = ", "
- fmt.Fprint(&buf, key)
- if values {
- fmt.Fprintf(&buf, ": %q", value)
- }
- })
- fmt.Fprint(&buf, "}")
- return buf.String()
-}
-
-// String returns a string representation of the map's entries.
-// Values are printed using fmt.Sprintf("%v", v).
-// Order is unspecified.
-func (m *Map) String() string {
- return m.toString(true)
-}
-
-// KeysString returns a string representation of the map's key set.
-// Order is unspecified.
-func (m *Map) KeysString() string {
- return m.toString(false)
-}
-
-////////////////////////////////////////////////////////////////////////
-// Hasher
-
-// A Hasher maps each type to its hash value.
-// For efficiency, a hasher uses memoization; thus its memory
-// footprint grows monotonically over time.
-// Hashers are not thread-safe.
-// Hashers have reference semantics.
-// Call MakeHasher to create a Hasher.
-type Hasher struct {
- memo map[types.Type]uint32
-
- // ptrMap records pointer identity.
- ptrMap map[any]uint32
-
- // sigTParams holds type parameters from the signature being hashed.
- // Signatures are considered identical modulo renaming of type parameters, so
- // within the scope of a signature type the identity of the signature's type
- // parameters is just their index.
- //
- // Since the language does not currently support referring to uninstantiated
- // generic types or functions, and instantiated signatures do not have type
- // parameter lists, we should never encounter a second non-empty type
- // parameter list when hashing a generic signature.
- sigTParams *types.TypeParamList
-}
-
-// MakeHasher returns a new Hasher instance.
-func MakeHasher() Hasher {
- return Hasher{
- memo: make(map[types.Type]uint32),
- ptrMap: make(map[any]uint32),
- sigTParams: nil,
- }
-}
-
-// Hash computes a hash value for the given type t such that
-// Identical(t, t') => Hash(t) == Hash(t').
-func (h Hasher) Hash(t types.Type) uint32 {
- hash, ok := h.memo[t]
- if !ok {
- hash = h.hashFor(t)
- h.memo[t] = hash
- }
- return hash
-}
-
-// hashString computes the Fowler–Noll–Vo hash of s.
-func hashString(s string) uint32 {
- var h uint32
- for i := 0; i < len(s); i++ {
- h ^= uint32(s[i])
- h *= 16777619
- }
- return h
-}
-
-// hashFor computes the hash of t.
-func (h Hasher) hashFor(t types.Type) uint32 {
- // See Identical for rationale.
- switch t := t.(type) {
- case *types.Basic:
- return uint32(t.Kind())
-
- case *types.Alias:
- return h.Hash(types.Unalias(t))
-
- case *types.Array:
- return 9043 + 2*uint32(t.Len()) + 3*h.Hash(t.Elem())
-
- case *types.Slice:
- return 9049 + 2*h.Hash(t.Elem())
-
- case *types.Struct:
- var hash uint32 = 9059
- for i, n := 0, t.NumFields(); i < n; i++ {
- f := t.Field(i)
- if f.Anonymous() {
- hash += 8861
- }
- hash += hashString(t.Tag(i))
- hash += hashString(f.Name()) // (ignore f.Pkg)
- hash += h.Hash(f.Type())
- }
- return hash
-
- case *types.Pointer:
- return 9067 + 2*h.Hash(t.Elem())
-
- case *types.Signature:
- var hash uint32 = 9091
- if t.Variadic() {
- hash *= 8863
- }
-
- // Use a separate hasher for types inside of the signature, where type
- // parameter identity is modified to be (index, constraint). We must use a
- // new memo for this hasher as type identity may be affected by this
- // masking. For example, in func[T any](*T), the identity of *T depends on
- // whether we are mapping the argument in isolation, or recursively as part
- // of hashing the signature.
- //
- // We should never encounter a generic signature while hashing another
- // generic signature, but defensively set sigTParams only if h.mask is
- // unset.
- tparams := t.TypeParams()
- if h.sigTParams == nil && tparams.Len() != 0 {
- h = Hasher{
- // There may be something more efficient than discarding the existing
- // memo, but it would require detecting whether types are 'tainted' by
- // references to type parameters.
- memo: make(map[types.Type]uint32),
- // Re-using ptrMap ensures that pointer identity is preserved in this
- // hasher.
- ptrMap: h.ptrMap,
- sigTParams: tparams,
- }
- }
-
- for i := 0; i < tparams.Len(); i++ {
- tparam := tparams.At(i)
- hash += 7 * h.Hash(tparam.Constraint())
- }
-
- return hash + 3*h.hashTuple(t.Params()) + 5*h.hashTuple(t.Results())
-
- case *types.Union:
- return h.hashUnion(t)
-
- case *types.Interface:
- // Interfaces are identical if they have the same set of methods, with
- // identical names and types, and they have the same set of type
- // restrictions. See go/types.identical for more details.
- var hash uint32 = 9103
-
- // Hash methods.
- for i, n := 0, t.NumMethods(); i < n; i++ {
- // Method order is not significant.
- // Ignore m.Pkg().
- m := t.Method(i)
- // Use shallow hash on method signature to
- // avoid anonymous interface cycles.
- hash += 3*hashString(m.Name()) + 5*h.shallowHash(m.Type())
- }
-
- // Hash type restrictions.
- terms, err := typeparams.InterfaceTermSet(t)
- // if err != nil t has invalid type restrictions.
- if err == nil {
- hash += h.hashTermSet(terms)
- }
-
- return hash
-
- case *types.Map:
- return 9109 + 2*h.Hash(t.Key()) + 3*h.Hash(t.Elem())
-
- case *types.Chan:
- return 9127 + 2*uint32(t.Dir()) + 3*h.Hash(t.Elem())
-
- case *types.Named:
- hash := h.hashPtr(t.Obj())
- targs := t.TypeArgs()
- for i := 0; i < targs.Len(); i++ {
- targ := targs.At(i)
- hash += 2 * h.Hash(targ)
- }
- return hash
-
- case *types.TypeParam:
- return h.hashTypeParam(t)
-
- case *types.Tuple:
- return h.hashTuple(t)
- }
-
- panic(fmt.Sprintf("%T: %v", t, t))
-}
-
-func (h Hasher) hashTuple(tuple *types.Tuple) uint32 {
- // See go/types.identicalTypes for rationale.
- n := tuple.Len()
- hash := 9137 + 2*uint32(n)
- for i := 0; i < n; i++ {
- hash += 3 * h.Hash(tuple.At(i).Type())
- }
- return hash
-}
-
-func (h Hasher) hashUnion(t *types.Union) uint32 {
- // Hash type restrictions.
- terms, err := typeparams.UnionTermSet(t)
- // if err != nil t has invalid type restrictions. Fall back on a non-zero
- // hash.
- if err != nil {
- return 9151
- }
- return h.hashTermSet(terms)
-}
-
-func (h Hasher) hashTermSet(terms []*types.Term) uint32 {
- hash := 9157 + 2*uint32(len(terms))
- for _, term := range terms {
- // term order is not significant.
- termHash := h.Hash(term.Type())
- if term.Tilde() {
- termHash *= 9161
- }
- hash += 3 * termHash
- }
- return hash
-}
-
-// hashTypeParam returns a hash of the type parameter t, with a hash value
-// depending on whether t is contained in h.sigTParams.
-//
-// If h.sigTParams is set and contains t, then we are in the process of hashing
-// a signature, and the hash value of t must depend only on t's index and
-// constraint: signatures are considered identical modulo type parameter
-// renaming. To avoid infinite recursion, we only hash the type parameter
-// index, and rely on types.Identical to handle signatures where constraints
-// are not identical.
-//
-// Otherwise the hash of t depends only on t's pointer identity.
-func (h Hasher) hashTypeParam(t *types.TypeParam) uint32 {
- if h.sigTParams != nil {
- i := t.Index()
- if i >= 0 && i < h.sigTParams.Len() && t == h.sigTParams.At(i) {
- return 9173 + 3*uint32(i)
- }
- }
- return h.hashPtr(t.Obj())
-}
-
-// hashPtr hashes the pointer identity of ptr. It uses h.ptrMap to ensure that
-// pointers values are not dependent on the GC.
-func (h Hasher) hashPtr(ptr any) uint32 {
- if hash, ok := h.ptrMap[ptr]; ok {
- return hash
- }
- hash := uint32(reflect.ValueOf(ptr).Pointer())
- h.ptrMap[ptr] = hash
- return hash
-}
-
-// shallowHash computes a hash of t without looking at any of its
-// element Types, to avoid potential anonymous cycles in the types of
-// interface methods.
-//
-// When an unnamed non-empty interface type appears anywhere among the
-// arguments or results of an interface method, there is a potential
-// for endless recursion. Consider:
-//
-// type X interface { m() []*interface { X } }
-//
-// The problem is that the Methods of the interface in m's result type
-// include m itself; there is no mention of the named type X that
-// might help us break the cycle.
-// (See comment in go/types.identical, case *Interface, for more.)
-func (h Hasher) shallowHash(t types.Type) uint32 {
- // t is the type of an interface method (Signature),
- // its params or results (Tuples), or their immediate
- // elements (mostly Slice, Pointer, Basic, Named),
- // so there's no need to optimize anything else.
- switch t := t.(type) {
- case *types.Alias:
- return h.shallowHash(types.Unalias(t))
-
- case *types.Signature:
- var hash uint32 = 604171
- if t.Variadic() {
- hash *= 971767
- }
- // The Signature/Tuple recursion is always finite
- // and invariably shallow.
- return hash + 1062599*h.shallowHash(t.Params()) + 1282529*h.shallowHash(t.Results())
-
- case *types.Tuple:
- n := t.Len()
- hash := 9137 + 2*uint32(n)
- for i := 0; i < n; i++ {
- hash += 53471161 * h.shallowHash(t.At(i).Type())
- }
- return hash
-
- case *types.Basic:
- return 45212177 * uint32(t.Kind())
-
- case *types.Array:
- return 1524181 + 2*uint32(t.Len())
-
- case *types.Slice:
- return 2690201
-
- case *types.Struct:
- return 3326489
-
- case *types.Pointer:
- return 4393139
-
- case *types.Union:
- return 562448657
-
- case *types.Interface:
- return 2124679 // no recursion here
-
- case *types.Map:
- return 9109
-
- case *types.Chan:
- return 9127
-
- case *types.Named:
- return h.hashPtr(t.Obj())
-
- case *types.TypeParam:
- return h.hashPtr(t.Obj())
- }
- panic(fmt.Sprintf("shallowHash: %T: %v", t, t))
-}
diff --git a/vendor/golang.org/x/tools/go/types/typeutil/methodsetcache.go b/vendor/golang.org/x/tools/go/types/typeutil/methodsetcache.go
deleted file mode 100644
index f7666028f..000000000
--- a/vendor/golang.org/x/tools/go/types/typeutil/methodsetcache.go
+++ /dev/null
@@ -1,71 +0,0 @@
-// Copyright 2014 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.
-
-// This file implements a cache of method sets.
-
-package typeutil
-
-import (
- "go/types"
- "sync"
-)
-
-// A MethodSetCache records the method set of each type T for which
-// MethodSet(T) is called so that repeat queries are fast.
-// The zero value is a ready-to-use cache instance.
-type MethodSetCache struct {
- mu sync.Mutex
- named map[*types.Named]struct{ value, pointer *types.MethodSet } // method sets for named N and *N
- others map[types.Type]*types.MethodSet // all other types
-}
-
-// MethodSet returns the method set of type T. It is thread-safe.
-//
-// If cache is nil, this function is equivalent to types.NewMethodSet(T).
-// Utility functions can thus expose an optional *MethodSetCache
-// parameter to clients that care about performance.
-func (cache *MethodSetCache) MethodSet(T types.Type) *types.MethodSet {
- if cache == nil {
- return types.NewMethodSet(T)
- }
- cache.mu.Lock()
- defer cache.mu.Unlock()
-
- switch T := types.Unalias(T).(type) {
- case *types.Named:
- return cache.lookupNamed(T).value
-
- case *types.Pointer:
- if N, ok := types.Unalias(T.Elem()).(*types.Named); ok {
- return cache.lookupNamed(N).pointer
- }
- }
-
- // all other types
- // (The map uses pointer equivalence, not type identity.)
- mset := cache.others[T]
- if mset == nil {
- mset = types.NewMethodSet(T)
- if cache.others == nil {
- cache.others = make(map[types.Type]*types.MethodSet)
- }
- cache.others[T] = mset
- }
- return mset
-}
-
-func (cache *MethodSetCache) lookupNamed(named *types.Named) struct{ value, pointer *types.MethodSet } {
- if cache.named == nil {
- cache.named = make(map[*types.Named]struct{ value, pointer *types.MethodSet })
- }
- // Avoid recomputing mset(*T) for each distinct Pointer
- // instance whose underlying type is a named type.
- msets, ok := cache.named[named]
- if !ok {
- msets.value = types.NewMethodSet(named)
- msets.pointer = types.NewMethodSet(types.NewPointer(named))
- cache.named[named] = msets
- }
- return msets
-}
diff --git a/vendor/golang.org/x/tools/go/types/typeutil/ui.go b/vendor/golang.org/x/tools/go/types/typeutil/ui.go
deleted file mode 100644
index 9dda6a25d..000000000
--- a/vendor/golang.org/x/tools/go/types/typeutil/ui.go
+++ /dev/null
@@ -1,53 +0,0 @@
-// Copyright 2014 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.
-
-package typeutil
-
-// This file defines utilities for user interfaces that display types.
-
-import (
- "go/types"
-)
-
-// IntuitiveMethodSet returns the intuitive method set of a type T,
-// which is the set of methods you can call on an addressable value of
-// that type.
-//
-// The result always contains MethodSet(T), and is exactly MethodSet(T)
-// for interface types and for pointer-to-concrete types.
-// For all other concrete types T, the result additionally
-// contains each method belonging to *T if there is no identically
-// named method on T itself.
-//
-// This corresponds to user intuition about method sets;
-// this function is intended only for user interfaces.
-//
-// The order of the result is as for types.MethodSet(T).
-func IntuitiveMethodSet(T types.Type, msets *MethodSetCache) []*types.Selection {
- isPointerToConcrete := func(T types.Type) bool {
- ptr, ok := types.Unalias(T).(*types.Pointer)
- return ok && !types.IsInterface(ptr.Elem())
- }
-
- var result []*types.Selection
- mset := msets.MethodSet(T)
- if types.IsInterface(T) || isPointerToConcrete(T) {
- for i, n := 0, mset.Len(); i < n; i++ {
- result = append(result, mset.At(i))
- }
- } else {
- // T is some other concrete type.
- // Report methods of T and *T, preferring those of T.
- pmset := msets.MethodSet(types.NewPointer(T))
- for i, n := 0, pmset.Len(); i < n; i++ {
- meth := pmset.At(i)
- if m := mset.Lookup(meth.Obj().Pkg(), meth.Obj().Name()); m != nil {
- meth = m
- }
- result = append(result, meth)
- }
-
- }
- return result
-}