| 1 | // Copyright 2014 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 typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/ |
| 6 | |
| 7 | // This file defines utilities for user interfaces that display types. |
| 8 | |
| 9 | import ( |
| 10 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 11 | ) |
| 12 | |
| 13 | // IntuitiveMethodSet returns the intuitive method set of a type T, |
| 14 | // which is the set of methods you can call on an addressable value of |
| 15 | // that type. |
| 16 | // |
| 17 | // The result always contains MethodSet(T), and is exactly MethodSet(T) |
| 18 | // for interface types and for pointer-to-concrete types. |
| 19 | // For all other concrete types T, the result additionally |
| 20 | // contains each method belonging to *T if there is no identically |
| 21 | // named method on T itself. |
| 22 | // |
| 23 | // This corresponds to user intuition about method sets; |
| 24 | // this function is intended only for user interfaces. |
| 25 | // |
| 26 | // The order of the result is as for types.MethodSet(T). |
| 27 | func IntuitiveMethodSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/IntuitiveMethodSet().(T typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#, msets *MethodSetCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#) []*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Selectionscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection# { |
| 28 | isPointerToConcrete := func(T typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#) bool { |
| 29 | ptr, ok := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unaliasscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Unalias().(T).(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Pointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#) |
| 30 | return ok && !typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.IsInterfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/IsInterface().(ptr.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#Elem().()) |
| 31 | } |
| 32 | |
| 33 | var result []*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Selectionscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection# |
| 34 | mset := msets.MethodSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#MethodSet().(T) |
| 35 | if typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.IsInterfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/IsInterface().(T) || isPointerToConcrete(T) { |
| 36 | for i, n := 0, mset.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#Len().(); i < n; i++ { |
| 37 | result = append(result, mset.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#At().(i)) |
| 38 | } |
| 39 | } else { |
| 40 | // T is some other concrete type. |
| 41 | // Report methods of T and *T, preferring those of T. |
| 42 | pmset := msets.MethodSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#MethodSet().(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewPointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewPointer().(T)) |
| 43 | for i, n := 0, pmset.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#Len().(); i < n; i++ { |
| 44 | meth := pmset.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#At().(i) |
| 45 | if m := mset.Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#Lookup().(meth.Objscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#Obj().().Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().(), meth.Objscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Selection#Obj().().Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().()); m != nil { |
| 46 | meth = m |
| 47 | } |
| 48 | result = append(result, meth) |
| 49 | } |
| 50 | |
| 51 | } |
| 52 | return result |
| 53 | } |