| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"go/token" |
| 12 |
"strings" |
| 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/cache/parsego" |
| 17 |
"golang.org/x/tools/gopls/internal/protocol" |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
var ErrNoLinkname = errors.New("no linkname directive found") |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
func linknameDefinition(ctx context.Context, snapshot *cache.Snapshot, m *protocol.Mapper, from protocol.Range) ([]protocol.Location, error) { |
| 28 |
pkgPath, name, _ := parseLinkname(m, from) |
| 29 |
if pkgPath == "" { |
| 30 |
return nil, ErrNoLinkname |
| 31 |
} |
| 32 |
|
| 33 |
_, pgf, pos, err := findLinkname(ctx, snapshot, PackagePath(pkgPath), name) |
| 34 |
if err != nil { |
| 35 |
return nil, fmt.Errorf("find linkname: %w", err) |
| 36 |
} |
| 37 |
loc, err := pgf.PosLocation(pos, pos+token.Pos(len(name))) |
| 38 |
if err != nil { |
| 39 |
return nil, fmt.Errorf("location of linkname: %w", err) |
| 40 |
} |
| 41 |
return []protocol.Location{loc}, nil |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
func parseLinkname(m *protocol.Mapper, rng protocol.Range) (pkgPath, name string, targetOffset int) { |
| 54 |
if rng.Start.Line != rng.End.Line { |
| 55 |
return "", "", 0 |
| 56 |
} |
| 57 |
|
| 58 |
lineStart, err := m.PositionOffset(protocol.Position{Line: rng.Start.Line, Character: 0}) |
| 59 |
if err != nil { |
| 60 |
return "", "", 0 |
| 61 |
} |
| 62 |
lineEnd, err := m.PositionOffset(protocol.Position{Line: rng.Start.Line + 1, Character: 0}) |
| 63 |
if err != nil { |
| 64 |
return "", "", 0 |
| 65 |
} |
| 66 |
|
| 67 |
directive := string(m.Content[lineStart:lineEnd]) |
| 68 |
|
| 69 |
if !strings.HasPrefix(directive, "//go:linkname") { |
| 70 |
return "", "", 0 |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
if i := strings.LastIndex(directive, "//"); i != 0 { |
| 75 |
directive = strings.TrimSpace(directive[:i]) |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
parts := strings.Fields(directive) |
| 81 |
if len(parts) != 3 { |
| 82 |
return "", "", 0 |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
startOffset, endOffset, err := m.RangeOffsets(rng) |
| 88 |
if err != nil { |
| 89 |
return "", "", 0 |
| 90 |
} |
| 91 |
end := lineStart + len(directive) |
| 92 |
start := end - len(parts[2]) |
| 93 |
if !(start <= startOffset && endOffset <= end) { |
| 94 |
return "", "", 0 |
| 95 |
} |
| 96 |
linkname := parts[2] |
| 97 |
|
| 98 |
|
| 99 |
dot := strings.LastIndexByte(linkname, '.') |
| 100 |
if dot < 0 { |
| 101 |
return "", "", 0 |
| 102 |
} |
| 103 |
|
| 104 |
return linkname[:dot], linkname[dot+1:], start |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
func findLinkname(ctx context.Context, snapshot *cache.Snapshot, pkgPath PackagePath, name string) (*cache.Package, *parsego.File, token.Pos, error) { |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
var pkgMeta *metadata.Package |
| 114 |
metas, err := snapshot.AllMetadata(ctx) |
| 115 |
if err != nil { |
| 116 |
return nil, nil, token.NoPos, err |
| 117 |
} |
| 118 |
metadata.RemoveIntermediateTestVariants(&metas) |
| 119 |
for _, meta := range metas { |
| 120 |
if meta.PkgPath == pkgPath { |
| 121 |
pkgMeta = meta |
| 122 |
break |
| 123 |
} |
| 124 |
} |
| 125 |
if pkgMeta == nil { |
| 126 |
return nil, nil, token.NoPos, fmt.Errorf("cannot find package %q", pkgPath) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
pkgs, err := snapshot.TypeCheck(ctx, pkgMeta.ID) |
| 131 |
if err != nil { |
| 132 |
return nil, nil, token.NoPos, err |
| 133 |
} |
| 134 |
pkg := pkgs[0] |
| 135 |
|
| 136 |
obj := pkg.Types().Scope().Lookup(name) |
| 137 |
if obj == nil { |
| 138 |
return nil, nil, token.NoPos, fmt.Errorf("package %q does not define %s", pkgPath, name) |
| 139 |
} |
| 140 |
|
| 141 |
pos := obj.Pos() |
| 142 |
pgf, err := pkg.FileEnclosing(pos) |
| 143 |
return pkg, pgf, pos, err |
| 144 |
} |