Debian Codegraph @

go/types/objectpath/main.go

raw symbols SCIP
1 // Copyright 2026 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build ignore
6
7 // The objectpath command is a debugging tool that prints
8 // the object path of each symbol declared in a package.
9 //
10 // An object path is a unique identifier for an exported Go language symbol
11 // (function, type, variable, etc) relative to its declaring package.
12 // See [golang.org/x/tools/go/types/objectpath] for details.
13 //
14 // Usage: go run ./go/types/objectpath/main.go [packages...]
15 package main
16
17 import (
18 "cmp"
19 "flag"
20 "fmt"
21 "go/types"
22 "log"
23 "maps"
24 "os"
25 "path/filepath"
26 "slices"
27 "strconv"
28
29 "golang.org/x/tools/go/packages"
30 "golang.org/x/tools/go/types/objectpath"
31 "golang.org/x/tools/internal/typesinternal"
32 )
33
34 func main() {
35 log.SetPrefix("objectpath: ")
36 log.SetFlags(0)
37 flag.Parse()
38
39 exitcode := 0
40
41 cfg := &packages.Config{Mode: packages.LoadSyntax}
42 pkgs, err := packages.Load(cfg, flag.Args()...)
43 if err != nil {
44 log.Fatalf("packages.Load: %v", err)
45 }
46 if packages.PrintErrors(pkgs) > 0 {
47 exitcode = 1
48 }
49
50 for _, pkg := range pkgs {
51 var (
52 fset = pkg.Fset
53 objectpathFor = new(objectpath.Encoder).For
54 )
55
56 // objString formats a symbol relative to this package.
57 objString := func(obj types.Object) string {
58 return types.ObjectString(obj, func(p *types.Package) string {
59 if p == pkg.Types {
60 return ""
61 }
62 return p.Name()
63 })
64 }
65
66 // print prints a symbol's name, declared location, and object path.
67 print := func(obj types.Object) {
68 var got string
69 path, err := objectpathFor(obj)
70 if err != nil {
71 if mayFail(obj) {
72 return
73 }
74 exitcode = 1
75 got = "error: " + err.Error()
76 } else {
77 got = strconv.Quote(string(path))
78 }
79
80 posn := fset.Position(obj.Pos())
81 posn.Filename = filepath.Base(posn.Filename)
82
83 fmt.Printf("%20s: %v = %s\n", posn.String(), objString(obj), got)
84 }
85
86 // Gather all objects declared in this package, in source order.
87 objs := slices.Collect(maps.Values(pkg.TypesInfo.Defs))
88 objs = slices.DeleteFunc(objs, func(obj types.Object) bool {
89 return obj == nil
90 })
91 slices.SortFunc(objs, func(x, y types.Object) int {
92 return cmp.Compare(x.Pos(), y.Pos())
93 })
94
95 // Show each object's path.
96 for _, obj := range objs {
97 if obj != nil {
98 print(obj)
99 }
100 }
101 }
102
103 if exitcode != 0 {
104 log.Printf("there were errors")
105 }
106 os.Exit(exitcode)
107 }
108
109 // mayFail reports whether [objectpath.Encoder.For] is
110 // permitted to fail for this kind of symbol.
111 func mayFail(obj types.Object) bool {
112 // "The For function guarantees to return a path only for the following objects:
113 // - package-level types
114 // - exported package-level non-types
115 // - methods
116 // - parameter and result variables
117 // - struct fields"
118 if typesinternal.IsPackageLevel(obj) && is[*types.TypeName](obj) {
119 return false // package-level types
120 }
121 if obj.Exported() && typesinternal.IsPackageLevel(obj) && !is[*types.TypeName](obj) {
122 return false // exported package-level non-types
123 }
124
125 // The remaining rules are inaccurate: clearly,
126 // - local interface methods,
127 // - params/results of local functions, and
128 // - fields of local structs
129 // cannot have object paths.
130 // So don't enforce them for now.
131 // TODO(adonovan): clarify the documentation and update this check.
132 //
133 // if fn, ok := obj.(*types.Func); ok && fn.Signature().Recv() != nil {
134 // return false // methods
135 // }
136 // if v, ok := obj.(*types.Var); ok {
137 // case types.ParamVar, types.ResultVar, types.RecvVar:
138 // return false // parameter and result variables
139 // case types.FieldVar:
140 // return false // struct fields
141 // }
142 // }
143
144 return true
145 }
146
147 func is[T any](x any) bool {
148 _, ok := x.(T)
149 return ok
150 }