File: util.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (96 lines) | stat: -rw-r--r-- 2,433 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2023 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 testinggoroutine

import (
	"go/ast"
	"go/types"

	"golang.org/x/tools/go/ast/astutil"
	"golang.org/x/tools/internal/typeparams"
)

// AST and types utilities that not specific to testinggoroutines.

// localFunctionDecls returns a mapping from *types.Func to *ast.FuncDecl in files.
func localFunctionDecls(info *types.Info, files []*ast.File) func(*types.Func) *ast.FuncDecl {
	var fnDecls map[*types.Func]*ast.FuncDecl // computed lazily
	return func(f *types.Func) *ast.FuncDecl {
		if f != nil && fnDecls == nil {
			fnDecls = make(map[*types.Func]*ast.FuncDecl)
			for _, file := range files {
				for _, decl := range file.Decls {
					if fnDecl, ok := decl.(*ast.FuncDecl); ok {
						if fn, ok := info.Defs[fnDecl.Name].(*types.Func); ok {
							fnDecls[fn] = fnDecl
						}
					}
				}
			}
		}
		// TODO: set f = f.Origin() here.
		return fnDecls[f]
	}
}

// isMethodNamed returns true if f is a method defined
// in package with the path pkgPath with a name in names.
func isMethodNamed(f *types.Func, pkgPath string, names ...string) bool {
	if f == nil {
		return false
	}
	if f.Pkg() == nil || f.Pkg().Path() != pkgPath {
		return false
	}
	if f.Type().(*types.Signature).Recv() == nil {
		return false
	}
	for _, n := range names {
		if f.Name() == n {
			return true
		}
	}
	return false
}

func funcIdent(fun ast.Expr) *ast.Ident {
	switch fun := astutil.Unparen(fun).(type) {
	case *ast.IndexExpr, *ast.IndexListExpr:
		x, _, _, _ := typeparams.UnpackIndexExpr(fun) // necessary?
		id, _ := x.(*ast.Ident)
		return id
	case *ast.Ident:
		return fun
	default:
		return nil
	}
}

// funcLitInScope returns a FuncLit that id is at least initially assigned to.
//
// TODO: This is closely tied to id.Obj which is deprecated.
func funcLitInScope(id *ast.Ident) *ast.FuncLit {
	// Compare to (*ast.Object).Pos().
	if id.Obj == nil {
		return nil
	}
	var rhs ast.Expr
	switch d := id.Obj.Decl.(type) {
	case *ast.AssignStmt:
		for i, x := range d.Lhs {
			if ident, isIdent := x.(*ast.Ident); isIdent && ident.Name == id.Name && i < len(d.Rhs) {
				rhs = d.Rhs[i]
			}
		}
	case *ast.ValueSpec:
		for i, n := range d.Names {
			if n.Name == id.Name && i < len(d.Values) {
				rhs = d.Values[i]
			}
		}
	}
	lit, _ := rhs.(*ast.FuncLit)
	return lit
}