Debian Codegraph @

gopls/internal/goasm/definition.go

raw symbols SCIP
1 // Copyright 2025 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 goasm provides language-server features for files in Go
6 // assembly language (https://go.dev/doc/asm).
7 package goasm
8
9 import (
10 "context"
11 "fmt"
12 "go/token"
13
14 "golang.org/x/tools/gopls/internal/cache"
15 "golang.org/x/tools/gopls/internal/cache/metadata"
16 "golang.org/x/tools/gopls/internal/file"
17 "golang.org/x/tools/gopls/internal/protocol"
18 "golang.org/x/tools/gopls/internal/util/asm"
19 "golang.org/x/tools/gopls/internal/util/morestrings"
20 "golang.org/x/tools/internal/event"
21 )
22
23 // Definition handles the textDocument/definition request for Go assembly files.
24 func Definition(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, rng protocol.Range) ([]protocol.Location, error) {
25 ctx, done := event.Start(ctx, "goasm.Definition")
26 defer done()
27
28 mp, err := snapshot.NarrowestMetadataForFile(ctx, fh.URI())
29 if err != nil {
30 return nil, err
31 }
32
33 // Read the file.
34 content, err := fh.Content()
35 if err != nil {
36 return nil, err
37 }
38 mapper := protocol.NewMapper(fh.URI(), content)
39 start, end, err := mapper.RangeOffsets(rng)
40 if err != nil {
41 return nil, err
42 }
43
44 // Parse the assembly.
45 //
46 // TODO(adonovan): make this just another
47 // attribute of the type-checked cache.Package.
48 file := asm.Parse(content)
49
50 // Figure out the selected symbol.
51 // For now, just find the identifier around the cursor.
52 var found *asm.Ident
53 for _, id := range file.Idents {
54 if id.Offset <= start && end <= id.End() {
55 found = &id
56 break
57 }
58 }
59 if found == nil {
60 return nil, fmt.Errorf("not an identifier")
61 }
62
63 // Resolve a symbol with a "." prefix to the current package.
64 sym := found.Name
65 if sym != "" && sym[0] == '.' {
66 sym = string(mp.PkgPath) + sym
67 }
68
69 // package-qualified symbol?
70 if pkgpath, name, ok := morestrings.CutLast(sym, "."); ok {
71 // Find declaring package among dependencies.
72 //
73 // TODO(adonovan): assembly may legally reference
74 // non-dependencies. For example, sync/atomic calls
75 // internal/runtime/atomic. Perhaps we should search
76 // the entire metadata graph, but that's path-dependent.
77 var declaring *metadata.Package
78 for pkg := range snapshot.MetadataGraph().ForwardReflexiveTransitiveClosure(mp.ID) {
79 if pkg.PkgPath == metadata.PackagePath(pkgpath) {
80 declaring = pkg
81 break
82 }
83 }
84 if declaring == nil {
85 return nil, fmt.Errorf("package %q is not a dependency", pkgpath)
86 }
87
88 // Find declared symbol in syntax package.
89 pkgs, err := snapshot.TypeCheck(ctx, declaring.ID)
90 if err != nil {
91 return nil, err
92 }
93 pkg := pkgs[0]
94 def := pkg.Types().Scope().Lookup(name)
95 if def == nil {
96 return nil, fmt.Errorf("no symbol %q in package %q", name, pkgpath)
97 }
98
99 // Map position.
100 pos := def.Pos()
101 pgf, err := pkg.FileEnclosing(pos)
102 if err != nil {
103 return nil, err
104 }
105 loc, err := pgf.PosLocation(pos, pos+token.Pos(len(name)))
106 if err != nil {
107 return nil, err
108 }
109 return []protocol.Location{loc}, nil
110
111 } else {
112 // local symbols (funcs, vars, labels)
113 for _, id := range file.Idents {
114 if id.Name == found.Name &&
115 (id.Kind == asm.Text || id.Kind == asm.Global || id.Kind == asm.Label) {
116
117 loc, err := mapper.OffsetLocation(id.Offset, id.End())
118 if err != nil {
119 return nil, err
120 }
121 return []protocol.Location{loc}, nil
122 }
123 }
124 }
125
126 return nil, nil
127 }