Debian Codegraph @

gopls/internal/cache/symbols/symbols.go

raw symbols SCIP
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package symbols defines the serializable index of package symbols extracted
6 // from parsed package files.
7 package symbols
8
9 import (
10 "go/ast"
11 "go/token"
12 "go/types"
13 "strings"
14
15 "golang.org/x/tools/gopls/internal/cache/parsego"
16 "golang.org/x/tools/gopls/internal/protocol"
17 "golang.org/x/tools/gopls/internal/util/frob"
18 "golang.org/x/tools/internal/astutil"
19 )
20
21 // Symbol holds a precomputed symbol value. This is a subset of the information
22 // in the full protocol.SymbolInformation struct to reduce the size of each
23 // symbol.
24 type Symbol struct {
25 Name string
26 Kind protocol.SymbolKind
27 Range protocol.Range
28 }
29
30 // A Package holds information about symbols declared by each file of a
31 // package.
32 //
33 // The symbols included are: package-level declarations, and fields and methods
34 // of type declarations.
35 type Package struct {
36 Files []protocol.DocumentURI // package files
37 Symbols [][]Symbol // symbols in each file
38 }
39
40 var codec = frob.CodecFor[*Package]()
41
42 // Decode decodes data from [Package.Encode].
43 func Decode(data []byte) *Package {
44 return codec.Decode(data)
45 }
46
47 // Encode encodes the package.
48 func (pkg *Package) Encode() []byte {
49 return codec.Encode(pkg)
50 }
51
52 // New returns a new [Package] summarizing symbols in the given files.
53 func New(files []*parsego.File) *Package {
54 var (
55 uris []protocol.DocumentURI
56 symbols [][]Symbol
57 )
58 for _, pgf := range files {
59 uris = append(uris, pgf.URI)
60 syms := symbolizeFile(pgf)
61 symbols = append(symbols, syms)
62 }
63 return &Package{
64 Files: uris,
65 Symbols: symbols,
66 }
67 }
68
69 // symbolizeFile reads and parses a file and extracts symbols from it.
70 func symbolizeFile(pgf *parsego.File) []Symbol {
71 w := &symbolWalker{
72 nodeRange: pgf.NodeRange,
73 }
74
75 for _, decl := range pgf.File.Decls {
76 switch decl := decl.(type) {
77 case *ast.FuncDecl:
78 kind := protocol.Function
79 var recv *ast.Ident
80 if decl.Recv.NumFields() > 0 {
81 kind = protocol.Method
82 _, recv, _ = astutil.UnpackRecv(decl.Recv.List[0].Type)
83 }
84 w.declare(decl.Name.Name, kind, decl.Name, recv)
85
86 case *ast.GenDecl:
87 for _, spec := range decl.Specs {
88 switch spec := spec.(type) {
89 case *ast.TypeSpec:
90 kind := protocol.Class
91 switch spec.Type.(type) {
92 case *ast.InterfaceType:
93 kind = protocol.Interface
94 case *ast.StructType:
95 kind = protocol.Struct
96 case *ast.FuncType:
97 kind = protocol.Function
98 }
99 w.declare(spec.Name.Name, kind, spec.Name)
100 w.walkType(spec.Type, spec.Name)
101 case *ast.ValueSpec:
102 for _, name := range spec.Names {
103 kind := protocol.Variable
104 if decl.Tok == token.CONST {
105 kind = protocol.Constant
106 }
107 w.declare(name.Name, kind, name)
108 }
109 }
110 }
111 }
112 }
113
114 return w.symbols
115 }
116
117 type symbolWalker struct {
118 nodeRange func(node ast.Node) (protocol.Range, error) // for computing positions
119
120 symbols []Symbol
121 }
122
123 // declare declares a symbol of the specified name, kind, node location, and enclosing dotted path of identifiers.
124 func (w *symbolWalker) declare(name string, kind protocol.SymbolKind, node ast.Node, path ...*ast.Ident) {
125 var b strings.Builder
126 for _, ident := range path {
127 if ident != nil {
128 b.WriteString(ident.Name)
129 b.WriteString(".")
130 }
131 }
132 b.WriteString(name)
133
134 rng, err := w.nodeRange(node)
135 if err != nil {
136 // TODO(rfindley): establish an invariant that node positions cannot exceed
137 // the file. This is not currently the case--for example see
138 // golang/go#48300 (this can also happen due to phantom selectors).
139 //
140 // For now, we have nothing to do with this error.
141 return
142 }
143 sym := Symbol{
144 Name: b.String(),
145 Kind: kind,
146 Range: rng,
147 }
148 w.symbols = append(w.symbols, sym)
149 }
150
151 // walkType processes symbols related to a type expression. path is path of
152 // nested type identifiers to the type expression.
153 func (w *symbolWalker) walkType(typ ast.Expr, path ...*ast.Ident) {
154 switch st := typ.(type) {
155 case *ast.StructType:
156 for _, field := range st.Fields.List {
157 w.walkField(field, protocol.Field, protocol.Field, path...)
158 }
159 case *ast.InterfaceType:
160 for _, field := range st.Methods.List {
161 w.walkField(field, protocol.Interface, protocol.Method, path...)
162 }
163 }
164 }
165
166 // walkField processes symbols related to the struct field or interface method.
167 //
168 // unnamedKind and namedKind are the symbol kinds if the field is resp. unnamed
169 // or named. path is the path of nested identifiers containing the field.
170 func (w *symbolWalker) walkField(field *ast.Field, unnamedKind, namedKind protocol.SymbolKind, path ...*ast.Ident) {
171 if len(field.Names) == 0 {
172 switch typ := field.Type.(type) {
173 case *ast.SelectorExpr:
174 // embedded qualified type
175 w.declare(typ.Sel.Name, unnamedKind, field, path...)
176 default:
177 w.declare(types.ExprString(field.Type), unnamedKind, field, path...)
178 }
179 }
180 for _, name := range field.Names {
181 w.declare(name.Name, namedKind, name, path...)
182 w.walkType(field.Type, append(path, name)...)
183 }
184 }