| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"go/ast" |
| 12 |
"go/token" |
| 13 |
"go/types" |
| 14 |
"iter" |
| 15 |
"reflect" |
| 16 |
"slices" |
| 17 |
"strings" |
| 18 |
"sync" |
| 19 |
|
| 20 |
"golang.org/x/sync/errgroup" |
| 21 |
"golang.org/x/tools/go/ast/edge" |
| 22 |
"golang.org/x/tools/go/ast/inspector" |
| 23 |
"golang.org/x/tools/go/types/typeutil" |
| 24 |
"golang.org/x/tools/gopls/internal/cache" |
| 25 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 26 |
"golang.org/x/tools/gopls/internal/cache/methodsets" |
| 27 |
"golang.org/x/tools/gopls/internal/file" |
| 28 |
"golang.org/x/tools/gopls/internal/protocol" |
| 29 |
"golang.org/x/tools/gopls/internal/util/bug" |
| 30 |
"golang.org/x/tools/gopls/internal/util/safetoken" |
| 31 |
"golang.org/x/tools/internal/event" |
| 32 |
"golang.org/x/tools/internal/moreiters" |
| 33 |
"golang.org/x/tools/internal/typesinternal" |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func Implementation(ctx context.Context, snapshot *cache.Snapshot, f file.Handle, rng protocol.Range) ([]protocol.Location, error) { |
| 58 |
ctx, done := event.Start(ctx, "golang.Implementation") |
| 59 |
defer done() |
| 60 |
|
| 61 |
locs, err := implementations(ctx, snapshot, f, rng) |
| 62 |
if err != nil { |
| 63 |
return nil, err |
| 64 |
} |
| 65 |
slices.SortFunc(locs, protocol.CompareLocation) |
| 66 |
locs = slices.Compact(locs) |
| 67 |
return locs, nil |
| 68 |
} |
| 69 |
|
| 70 |
func implementations(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, rng protocol.Range) ([]protocol.Location, error) { |
| 71 |
|
| 72 |
pkg, pgf, err := NarrowestPackageForFile(ctx, snapshot, fh.URI()) |
| 73 |
if err != nil { |
| 74 |
return nil, err |
| 75 |
} |
| 76 |
start, end, err := pgf.RangePos(rng) |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
cur, _ := pgf.Cursor().FindByPos(start, end) |
| 81 |
|
| 82 |
|
| 83 |
if locs, err := implFuncs(pkg, cur, start, end); err != errNotHandled { |
| 84 |
return locs, err |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
var ( |
| 89 |
locsMu sync.Mutex |
| 90 |
locs []protocol.Location |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
const relation = methodsets.TypeRelation(0) |
| 98 |
err = implementationsMsets(ctx, snapshot, pkg, cur, relation, func(_ metadata.PackagePath, _ string, _ bool, loc protocol.Location) { |
| 99 |
locsMu.Lock() |
| 100 |
locs = append(locs, loc) |
| 101 |
locsMu.Unlock() |
| 102 |
}) |
| 103 |
return locs, err |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
type implYieldFunc func(pkgpath metadata.PackagePath, name string, abstract bool, loc protocol.Location) |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
func implementationsMsets(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, cur inspector.Cursor, rel methodsets.TypeRelation, yield implYieldFunc) error { |
| 123 |
|
| 124 |
|
| 125 |
obj, err := implementsObj(pkg.TypesInfo(), cur) |
| 126 |
if err != nil { |
| 127 |
return err |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
var ( |
| 134 |
declOffset int |
| 135 |
declURI protocol.DocumentURI |
| 136 |
localPkgs []*cache.Package |
| 137 |
) |
| 138 |
if obj.Pos().IsValid() { |
| 139 |
declPosn := safetoken.StartPosition(pkg.FileSet(), obj.Pos()) |
| 140 |
declOffset = declPosn.Offset |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
declURI = protocol.URIFromPath(declPosn.Filename) |
| 146 |
declMPs, err := snapshot.MetadataForFile(ctx, declURI, true) |
| 147 |
if err != nil { |
| 148 |
return err |
| 149 |
} |
| 150 |
if len(declMPs) == 0 { |
| 151 |
return fmt.Errorf("no packages for file %s", declURI) |
| 152 |
} |
| 153 |
ids := make([]PackageID, len(declMPs)) |
| 154 |
for i, mp := range declMPs { |
| 155 |
ids[i] = mp.ID |
| 156 |
} |
| 157 |
localPkgs, err = snapshot.TypeCheck(ctx, ids...) |
| 158 |
if err != nil { |
| 159 |
return err |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
pkg = nil |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
queryType, queryMethod := typeOrMethod(obj) |
| 168 |
if queryType == nil { |
| 169 |
return bug.Errorf("%s is not a type or method", obj.Name()) |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
key, hasMethods := methodsets.KeyOf(queryType) |
| 174 |
if !hasMethods { |
| 175 |
|
| 176 |
|
| 177 |
return nil |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
if rel == 0 { |
| 183 |
rel = cond(types.IsInterface(queryType), |
| 184 |
methodsets.Subtype, |
| 185 |
methodsets.Supertype) |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
globalMetas, err := snapshot.AllMetadata(ctx) |
| 198 |
if err != nil { |
| 199 |
return err |
| 200 |
} |
| 201 |
metadata.RemoveIntermediateTestVariants(&globalMetas) |
| 202 |
globalIDs := make([]PackageID, 0, len(globalMetas)) |
| 203 |
|
| 204 |
var pkgPath PackagePath |
| 205 |
if obj.Pkg() != nil { |
| 206 |
pkgPath = PackagePath(obj.Pkg().Path()) |
| 207 |
} |
| 208 |
for _, mp := range globalMetas { |
| 209 |
globalIDs = append(globalIDs, mp.ID) |
| 210 |
} |
| 211 |
indexes, err := snapshot.MethodSets(ctx, globalIDs...) |
| 212 |
if err != nil { |
| 213 |
return fmt.Errorf("querying method sets: %v", err) |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
var byPkgPath map[PackagePath]*methodsets.Index |
| 229 |
if queryMethod != nil { |
| 230 |
byPkgPath = make(map[PackagePath]*methodsets.Index, len(indexes)) |
| 231 |
for i, index := range indexes { |
| 232 |
if globalMetas[i].ForTest == "" { |
| 233 |
byPkgPath[index.PkgPath] = index |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
var group errgroup.Group |
| 240 |
|
| 241 |
|
| 242 |
for _, pkg := range localPkgs { |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
group.Go(func() error { |
| 248 |
pkgID := pkg.Metadata().ID |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
declFile, err := pkg.File(declURI) |
| 254 |
if err != nil { |
| 255 |
return err |
| 256 |
} |
| 257 |
pos, err := safetoken.Pos(declFile.Tok, declOffset) |
| 258 |
if err != nil { |
| 259 |
return err |
| 260 |
} |
| 261 |
curIdent, ok := declFile.Cursor().FindByPos(pos, pos) |
| 262 |
if !ok { |
| 263 |
return bug.Errorf("position not within file") |
| 264 |
} |
| 265 |
id, ok := curIdent.Node().(*ast.Ident) |
| 266 |
if !ok { |
| 267 |
return ErrNoIdentFound |
| 268 |
} |
| 269 |
if err := localImplementations(ctx, snapshot, pkg, id, rel, yield); err != nil { |
| 270 |
return fmt.Errorf("querying local implementations %q: %v", pkgID, err) |
| 271 |
} |
| 272 |
return nil |
| 273 |
}) |
| 274 |
} |
| 275 |
|
| 276 |
for _, index := range indexes { |
| 277 |
if index.PkgPath == pkgPath { |
| 278 |
continue |
| 279 |
} |
| 280 |
group.Go(func() error { |
| 281 |
for _, res := range index.Search(key, rel, queryMethod) { |
| 282 |
loc := res.Location |
| 283 |
if loc.Filename == "" { |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
if declIdx, ok := byPkgPath[PackagePath(res.PkgPath)]; ok { |
| 288 |
loc, _ = declIdx.LocationOf(res.ObjectPath) |
| 289 |
} |
| 290 |
if loc.Filename == "" { |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
bug.Reportf("methodsets: no location for %s.%s", res.PkgPath, res.ObjectPath) |
| 298 |
continue |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
group.Go(func() error { |
| 303 |
ploc, err := offsetToLocation(ctx, snapshot, loc.Filename, loc.Start, loc.End) |
| 304 |
if err != nil { |
| 305 |
return err |
| 306 |
} |
| 307 |
yield(index.PkgPath, res.TypeName, res.IsInterface, ploc) |
| 308 |
return nil |
| 309 |
}) |
| 310 |
} |
| 311 |
return nil |
| 312 |
}) |
| 313 |
} |
| 314 |
return group.Wait() |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
func typeOrMethod(obj types.Object) (types.Type, *types.Func) { |
| 324 |
switch obj := obj.(type) { |
| 325 |
case *types.TypeName: |
| 326 |
return obj.Type(), nil |
| 327 |
case *types.Func: |
| 328 |
|
| 329 |
if recv := obj.Signature().Recv(); recv != nil { |
| 330 |
return recv.Type(), obj |
| 331 |
} |
| 332 |
} |
| 333 |
return nil, nil |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
func offsetToLocation(ctx context.Context, snapshot *cache.Snapshot, filename string, start, end int) (protocol.Location, error) { |
| 339 |
uri := protocol.URIFromPath(filename) |
| 340 |
fh, err := snapshot.ReadFile(ctx, uri) |
| 341 |
if err != nil { |
| 342 |
return protocol.Location{}, err |
| 343 |
} |
| 344 |
content, err := fh.Content() |
| 345 |
if err != nil { |
| 346 |
return protocol.Location{}, err |
| 347 |
} |
| 348 |
m := protocol.NewMapper(uri, content) |
| 349 |
return m.OffsetLocation(start, end) |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
func implementsObj(info *types.Info, cur inspector.Cursor) (types.Object, error) { |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
id, ok := cur.Node().(*ast.Ident) |
| 365 |
if !ok { |
| 366 |
return nil, ErrNoIdentFound |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
obj := info.Uses[id] |
| 371 |
if obj == nil { |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
obj = info.Defs[id] |
| 376 |
} |
| 377 |
switch obj := obj.(type) { |
| 378 |
case *types.TypeName: |
| 379 |
|
| 380 |
case *types.Func: |
| 381 |
if obj.Signature().Recv() == nil { |
| 382 |
return nil, fmt.Errorf("%s is a function, not a method (query at 'func' token to find matching signatures)", id.Name) |
| 383 |
} |
| 384 |
case nil: |
| 385 |
return nil, fmt.Errorf("%s denotes unknown object", id.Name) |
| 386 |
default: |
| 387 |
|
| 388 |
kind := strings.ToLower(strings.TrimPrefix(reflect.TypeOf(obj).String(), "*types.")) |
| 389 |
|
| 390 |
return nil, fmt.Errorf("%s is a %s, not a type", id.Name, kind) |
| 391 |
} |
| 392 |
|
| 393 |
return obj, nil |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
func localImplementations(ctx context.Context, snapshot *cache.Snapshot, pkg *cache.Package, id *ast.Ident, rel methodsets.TypeRelation, yield implYieldFunc) error { |
| 411 |
queryType, queryMethod := typeOrMethod(pkg.TypesInfo().Defs[id]) |
| 412 |
if queryType == nil { |
| 413 |
return bug.Errorf("can't find corresponding symbol for %q in package %q", id.Name, pkg) |
| 414 |
} |
| 415 |
queryType = methodsets.EnsurePointer(queryType) |
| 416 |
|
| 417 |
var msets typeutil.MethodSetCache |
| 418 |
|
| 419 |
matches := func(candidateType types.Type) bool { |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
var got methodsets.TypeRelation |
| 427 |
if rel&methodsets.Supertype != 0 && implements(&msets, queryType, candidateType) { |
| 428 |
got |= methodsets.Supertype |
| 429 |
} |
| 430 |
if rel&methodsets.Subtype != 0 && implements(&msets, candidateType, queryType) { |
| 431 |
got |= methodsets.Subtype |
| 432 |
} |
| 433 |
return got != 0 |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
for _, pgf := range pkg.CompiledGoFiles() { |
| 438 |
for cur := range pgf.Cursor().Preorder((*ast.TypeSpec)(nil)) { |
| 439 |
spec := cur.Node().(*ast.TypeSpec) |
| 440 |
if spec.Name == id { |
| 441 |
continue |
| 442 |
} |
| 443 |
def := pkg.TypesInfo().Defs[spec.Name] |
| 444 |
if def == nil { |
| 445 |
continue |
| 446 |
} |
| 447 |
if def.(*types.TypeName).IsAlias() { |
| 448 |
continue |
| 449 |
} |
| 450 |
candidateType := methodsets.EnsurePointer(def.Type()) |
| 451 |
if !matches(candidateType) { |
| 452 |
continue |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
mset := msets.MethodSet(candidateType) |
| 458 |
if mset.Len() == 0 { |
| 459 |
continue |
| 460 |
} |
| 461 |
|
| 462 |
isInterface := types.IsInterface(def.Type()) |
| 463 |
|
| 464 |
if queryMethod == nil { |
| 465 |
|
| 466 |
loc := mustLocation(pgf, spec.Name) |
| 467 |
yield(pkg.Metadata().PkgPath, spec.Name.Name, isInterface, loc) |
| 468 |
continue |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
for method := range mset.Methods() { |
| 478 |
m := method.Obj() |
| 479 |
if m.Pos() == id.Pos() { |
| 480 |
continue |
| 481 |
} |
| 482 |
if m.Id() == queryMethod.Id() { |
| 483 |
posn := safetoken.StartPosition(pkg.FileSet(), m.Pos()) |
| 484 |
loc, err := offsetToLocation(ctx, snapshot, posn.Filename, posn.Offset, posn.Offset+len(m.Name())) |
| 485 |
if err != nil { |
| 486 |
return err |
| 487 |
} |
| 488 |
yield(pkg.Metadata().PkgPath, m.Name(), isInterface, loc) |
| 489 |
break |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
if matches(errorType) { |
| 501 |
loc, err := errorLocation(ctx, snapshot) |
| 502 |
if err != nil { |
| 503 |
return err |
| 504 |
} |
| 505 |
yield("", "error", true, loc) |
| 506 |
} |
| 507 |
|
| 508 |
return nil |
| 509 |
} |
| 510 |
|
| 511 |
var errorType = types.Universe.Lookup("error").Type() |
| 512 |
|
| 513 |
|
| 514 |
func errorLocation(ctx context.Context, snapshot *cache.Snapshot) (protocol.Location, error) { |
| 515 |
pgf, err := snapshot.BuiltinFile(ctx) |
| 516 |
if err != nil { |
| 517 |
return protocol.Location{}, err |
| 518 |
} |
| 519 |
for _, decl := range pgf.File.Decls { |
| 520 |
if decl, ok := decl.(*ast.GenDecl); ok { |
| 521 |
for _, spec := range decl.Specs { |
| 522 |
if spec, ok := spec.(*ast.TypeSpec); ok && spec.Name.Name == "error" { |
| 523 |
return pgf.NodeLocation(spec.Name) |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
return protocol.Location{}, fmt.Errorf("built-in error type not found") |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
func implements(msets *typeutil.MethodSetCache, x, y types.Type) bool { |
| 535 |
if !types.IsInterface(y) { |
| 536 |
return false |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
ymset := msets.MethodSet(y) |
| 548 |
for method := range ymset.Methods() { |
| 549 |
ym := method.Obj().(*types.Func) |
| 550 |
|
| 551 |
xobj, _, _ := types.LookupFieldOrMethod(x, false, ym.Pkg(), ym.Name()) |
| 552 |
xm, ok := xobj.(*types.Func) |
| 553 |
if !ok { |
| 554 |
return false |
| 555 |
} |
| 556 |
if !unify(xm.Signature(), ym.Signature(), nil) { |
| 557 |
return false |
| 558 |
} |
| 559 |
} |
| 560 |
return true |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
func unify(x, y types.Type, unifier map[*types.TypeParam]types.Type) bool { |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
bindings := map[*types.TypeParam]*types.Type{} |
| 609 |
|
| 610 |
|
| 611 |
for tp, t := range unifier { |
| 612 |
bindings[tp] = &t |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
bindingFor := func(tp *types.TypeParam) *types.Type { |
| 618 |
if tp == nil { |
| 619 |
return nil |
| 620 |
} |
| 621 |
b := bindings[tp] |
| 622 |
if b == nil { |
| 623 |
b = new(types.Type) |
| 624 |
bindings[tp] = b |
| 625 |
} |
| 626 |
return b |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
bind := func(b *types.Type, t types.Type) bool { |
| 631 |
for tp := range typeParams(t) { |
| 632 |
if b == bindings[tp] { |
| 633 |
return false |
| 634 |
} |
| 635 |
} |
| 636 |
*b = t |
| 637 |
return true |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
depth := 0 |
| 642 |
var uni func(x, y types.Type) bool |
| 643 |
uni = func(x, y types.Type) bool { |
| 644 |
|
| 645 |
|
| 646 |
depth++ |
| 647 |
defer func() { depth-- }() |
| 648 |
if depth > 100 { |
| 649 |
panic("unify: max depth exceeded") |
| 650 |
} |
| 651 |
|
| 652 |
x = types.Unalias(x) |
| 653 |
y = types.Unalias(y) |
| 654 |
|
| 655 |
tpx, _ := x.(*types.TypeParam) |
| 656 |
tpy, _ := y.(*types.TypeParam) |
| 657 |
if tpx != nil || tpy != nil { |
| 658 |
|
| 659 |
if tpx == tpy { |
| 660 |
return true |
| 661 |
} |
| 662 |
bx := bindingFor(tpx) |
| 663 |
by := bindingFor(tpy) |
| 664 |
|
| 665 |
|
| 666 |
if bx != nil && by != nil && *bx == nil && *by == nil { |
| 667 |
|
| 668 |
bindings[tpx] = by |
| 669 |
return true |
| 670 |
} |
| 671 |
|
| 672 |
if bx != nil && *bx != nil { |
| 673 |
x = *bx |
| 674 |
} |
| 675 |
if by != nil && *by != nil { |
| 676 |
y = *by |
| 677 |
} |
| 678 |
|
| 679 |
if bx != nil && *bx == nil { |
| 680 |
return bind(bx, y) |
| 681 |
} |
| 682 |
|
| 683 |
if by != nil && *by == nil { |
| 684 |
return bind(by, x) |
| 685 |
} |
| 686 |
|
| 687 |
return uni(x, y) |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
if reflect.TypeOf(x) != reflect.TypeOf(y) { |
| 693 |
return false |
| 694 |
} |
| 695 |
|
| 696 |
switch x := x.(type) { |
| 697 |
case *types.Array: |
| 698 |
y := y.(*types.Array) |
| 699 |
return x.Len() == y.Len() && |
| 700 |
uni(x.Elem(), y.Elem()) |
| 701 |
|
| 702 |
case *types.Basic: |
| 703 |
y := y.(*types.Basic) |
| 704 |
return x.Kind() == y.Kind() |
| 705 |
|
| 706 |
case *types.Chan: |
| 707 |
y := y.(*types.Chan) |
| 708 |
return x.Dir() == y.Dir() && |
| 709 |
uni(x.Elem(), y.Elem()) |
| 710 |
|
| 711 |
case *types.Interface: |
| 712 |
y := y.(*types.Interface) |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
return x.NumMethods() == y.NumMethods() |
| 723 |
|
| 724 |
case *types.Map: |
| 725 |
y := y.(*types.Map) |
| 726 |
return uni(x.Key(), y.Key()) && |
| 727 |
uni(x.Elem(), y.Elem()) |
| 728 |
|
| 729 |
case *types.Named: |
| 730 |
y := y.(*types.Named) |
| 731 |
if x.Origin() != y.Origin() { |
| 732 |
return false |
| 733 |
} |
| 734 |
xtargs := x.TypeArgs() |
| 735 |
ytargs := y.TypeArgs() |
| 736 |
if xtargs.Len() != ytargs.Len() { |
| 737 |
return false |
| 738 |
} |
| 739 |
for i := range xtargs.Len() { |
| 740 |
if !uni(xtargs.At(i), ytargs.At(i)) { |
| 741 |
return false |
| 742 |
} |
| 743 |
} |
| 744 |
return true |
| 745 |
|
| 746 |
case *types.Pointer: |
| 747 |
y := y.(*types.Pointer) |
| 748 |
return uni(x.Elem(), y.Elem()) |
| 749 |
|
| 750 |
case *types.Signature: |
| 751 |
y := y.(*types.Signature) |
| 752 |
return x.Variadic() == y.Variadic() && |
| 753 |
uni(x.Params(), y.Params()) && |
| 754 |
uni(x.Results(), y.Results()) |
| 755 |
|
| 756 |
case *types.Slice: |
| 757 |
y := y.(*types.Slice) |
| 758 |
return uni(x.Elem(), y.Elem()) |
| 759 |
|
| 760 |
case *types.Struct: |
| 761 |
y := y.(*types.Struct) |
| 762 |
if x.NumFields() != y.NumFields() { |
| 763 |
return false |
| 764 |
} |
| 765 |
for i := range x.NumFields() { |
| 766 |
xf := x.Field(i) |
| 767 |
yf := y.Field(i) |
| 768 |
if xf.Embedded() != yf.Embedded() || |
| 769 |
xf.Name() != yf.Name() || |
| 770 |
x.Tag(i) != y.Tag(i) || |
| 771 |
!xf.Exported() && xf.Pkg() != yf.Pkg() || |
| 772 |
!uni(xf.Type(), yf.Type()) { |
| 773 |
return false |
| 774 |
} |
| 775 |
} |
| 776 |
return true |
| 777 |
|
| 778 |
case *types.Tuple: |
| 779 |
y := y.(*types.Tuple) |
| 780 |
if x.Len() != y.Len() { |
| 781 |
return false |
| 782 |
} |
| 783 |
for i := range x.Len() { |
| 784 |
if !uni(x.At(i).Type(), y.At(i).Type()) { |
| 785 |
return false |
| 786 |
} |
| 787 |
} |
| 788 |
return true |
| 789 |
|
| 790 |
default: |
| 791 |
panic(fmt.Sprintf("unexpected Type %#v", x)) |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
if !uni(x, y) { |
| 796 |
clear(unifier) |
| 797 |
return false |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
if unifier != nil { |
| 802 |
for tparam, tptr := range bindings { |
| 803 |
unifier[tparam] = *tptr |
| 804 |
} |
| 805 |
} |
| 806 |
return true |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
func typeParams(t types.Type) iter.Seq[*types.TypeParam] { |
| 815 |
|
| 816 |
return func(yield func(*types.TypeParam) bool) { |
| 817 |
seen := map[*types.TypeParam]bool{} |
| 818 |
|
| 819 |
|
| 820 |
var tps func(types.Type) bool |
| 821 |
tps = func(t types.Type) bool { |
| 822 |
t = types.Unalias(t) |
| 823 |
|
| 824 |
switch t := t.(type) { |
| 825 |
case *types.TypeParam: |
| 826 |
if seen[t] { |
| 827 |
return true |
| 828 |
} |
| 829 |
seen[t] = true |
| 830 |
return yield(t) |
| 831 |
|
| 832 |
case *types.Basic: |
| 833 |
return true |
| 834 |
|
| 835 |
case *types.Array: |
| 836 |
return tps(t.Elem()) |
| 837 |
|
| 838 |
case *types.Chan: |
| 839 |
return tps(t.Elem()) |
| 840 |
|
| 841 |
case *types.Interface: |
| 842 |
|
| 843 |
return true |
| 844 |
|
| 845 |
case *types.Map: |
| 846 |
return tps(t.Key()) && tps(t.Elem()) |
| 847 |
|
| 848 |
case *types.Named: |
| 849 |
if t.Origin() == t { |
| 850 |
|
| 851 |
return moreiters.Every(t.TypeParams().TypeParams(), |
| 852 |
func(tp *types.TypeParam) bool { return tps(tp) }) |
| 853 |
} |
| 854 |
|
| 855 |
return moreiters.Every(t.TypeArgs().Types(), tps) |
| 856 |
|
| 857 |
case *types.Pointer: |
| 858 |
return tps(t.Elem()) |
| 859 |
|
| 860 |
case *types.Signature: |
| 861 |
return tps(t.Params()) && tps(t.Results()) |
| 862 |
|
| 863 |
case *types.Slice: |
| 864 |
return tps(t.Elem()) |
| 865 |
|
| 866 |
case *types.Struct: |
| 867 |
return moreiters.Every(t.Fields(), |
| 868 |
func(v *types.Var) bool { return tps(v.Type()) }) |
| 869 |
|
| 870 |
case *types.Tuple: |
| 871 |
return moreiters.Every(t.Variables(), |
| 872 |
func(v *types.Var) bool { return tps(v.Type()) }) |
| 873 |
|
| 874 |
default: |
| 875 |
panic(fmt.Sprintf("unexpected Type %#v", t)) |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
tps(t) |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
var ( |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
errNoObjectFound = errors.New("no object found") |
| 889 |
) |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
func implFuncs(pkg *cache.Package, curSel inspector.Cursor, start, end token.Pos) ([]protocol.Location, error) { |
| 934 |
info := pkg.TypesInfo() |
| 935 |
if info.Types == nil || info.Defs == nil || info.Uses == nil { |
| 936 |
panic("one of info.Types, .Defs or .Uses is nil") |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
for cur := range curSel.Enclosing( |
| 954 |
(*ast.FuncDecl)(nil), |
| 955 |
(*ast.FuncLit)(nil), |
| 956 |
(*ast.FuncType)(nil), |
| 957 |
(*ast.CallExpr)(nil), |
| 958 |
) { |
| 959 |
switch n := cur.Node().(type) { |
| 960 |
case *ast.FuncDecl, *ast.FuncLit: |
| 961 |
if inToken(n.Pos(), "func", start, end) { |
| 962 |
|
| 963 |
|
| 964 |
switch n := n.(type) { |
| 965 |
case *ast.FuncDecl: |
| 966 |
return funcUses(pkg, info.Defs[n.Name].Type()) |
| 967 |
case *ast.FuncLit: |
| 968 |
return funcUses(pkg, info.TypeOf(n.Type)) |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
case *ast.FuncType: |
| 973 |
if n.Func.IsValid() && inToken(n.Func, "func", start, end) && !beneathFuncDef(cur) { |
| 974 |
|
| 975 |
|
| 976 |
return funcDefs(pkg, info.TypeOf(n)) |
| 977 |
} |
| 978 |
|
| 979 |
case *ast.CallExpr: |
| 980 |
if inToken(n.Lparen, "(", start, end) { |
| 981 |
t := dynamicFuncCallType(info, n) |
| 982 |
if t == nil { |
| 983 |
return nil, fmt.Errorf("not a dynamic function call") |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
return funcDefs(pkg, t) |
| 988 |
} |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
return nil, errNotHandled |
| 995 |
} |
| 996 |
|
| 997 |
var errNotHandled = errors.New("not handled") |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
func funcUses(pkg *cache.Package, t types.Type) ([]protocol.Location, error) { |
| 1002 |
var locs []protocol.Location |
| 1003 |
|
| 1004 |
|
| 1005 |
for _, pgf := range pkg.CompiledGoFiles() { |
| 1006 |
for cur := range pgf.Cursor().Preorder((*ast.CallExpr)(nil), (*ast.FuncType)(nil)) { |
| 1007 |
var pos, end token.Pos |
| 1008 |
var ftyp types.Type |
| 1009 |
switch n := cur.Node().(type) { |
| 1010 |
case *ast.CallExpr: |
| 1011 |
ftyp = dynamicFuncCallType(pkg.TypesInfo(), n) |
| 1012 |
pos, end = n.Lparen, n.Lparen+token.Pos(len("(")) |
| 1013 |
|
| 1014 |
case *ast.FuncType: |
| 1015 |
if !beneathFuncDef(cur) { |
| 1016 |
|
| 1017 |
ftyp = pkg.TypesInfo().TypeOf(n) |
| 1018 |
pos, end = n.Func, n.Func+token.Pos(len("func")) |
| 1019 |
} |
| 1020 |
} |
| 1021 |
if ftyp == nil { |
| 1022 |
continue |
| 1023 |
} |
| 1024 |
if unify(t, ftyp, nil) { |
| 1025 |
loc, err := pgf.PosLocation(pos, end) |
| 1026 |
if err != nil { |
| 1027 |
return nil, err |
| 1028 |
} |
| 1029 |
locs = append(locs, loc) |
| 1030 |
} |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
return locs, nil |
| 1037 |
} |
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
func funcDefs(pkg *cache.Package, t types.Type) ([]protocol.Location, error) { |
| 1042 |
var locs []protocol.Location |
| 1043 |
|
| 1044 |
|
| 1045 |
for _, pgf := range pkg.CompiledGoFiles() { |
| 1046 |
for curFn := range pgf.Cursor().Preorder((*ast.FuncDecl)(nil), (*ast.FuncLit)(nil)) { |
| 1047 |
fn := curFn.Node() |
| 1048 |
var ftyp types.Type |
| 1049 |
switch fn := fn.(type) { |
| 1050 |
case *ast.FuncDecl: |
| 1051 |
ftyp = pkg.TypesInfo().Defs[fn.Name].Type() |
| 1052 |
case *ast.FuncLit: |
| 1053 |
ftyp = pkg.TypesInfo().TypeOf(fn) |
| 1054 |
} |
| 1055 |
if ftyp == nil { |
| 1056 |
continue |
| 1057 |
} |
| 1058 |
if unify(t, ftyp, nil) { |
| 1059 |
pos := fn.Pos() |
| 1060 |
loc, err := pgf.PosLocation(pos, pos+token.Pos(len("func"))) |
| 1061 |
if err != nil { |
| 1062 |
return nil, err |
| 1063 |
} |
| 1064 |
locs = append(locs, loc) |
| 1065 |
} |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
return locs, nil |
| 1077 |
} |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
func beneathFuncDef(cur inspector.Cursor) bool { |
| 1082 |
switch cur.ParentEdgeKind() { |
| 1083 |
case edge.FuncDecl_Type, edge.FuncLit_Type: |
| 1084 |
return true |
| 1085 |
} |
| 1086 |
return false |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
func dynamicFuncCallType(info *types.Info, call *ast.CallExpr) types.Type { |
| 1094 |
if typesinternal.ClassifyCall(info, call) == typesinternal.CallDynamic { |
| 1095 |
if tv, ok := info.Types[call.Fun]; ok { |
| 1096 |
return tv.Type.Underlying() |
| 1097 |
} |
| 1098 |
} |
| 1099 |
return nil |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
func inToken(tokPos token.Pos, tokStr string, start, end token.Pos) bool { |
| 1105 |
return tokPos <= start && end <= tokPos+token.Pos(len(tokStr)) |
| 1106 |
} |