Debian Codegraph @

go/types/typeutil/ui.go

raw symbols SCIP
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 typeutil
6
7 // This file defines utilities for user interfaces that display types.
8
9 import (
10 "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 IntuitiveMethodSet(T types.Type, msets *MethodSetCache) []*types.Selection {
28 isPointerToConcrete := func(T types.Type) bool {
29 ptr, ok := types.Unalias(T).(*types.Pointer)
30 return ok && !types.IsInterface(ptr.Elem())
31 }
32
33 var result []*types.Selection
34 mset := msets.MethodSet(T)
35 if types.IsInterface(T) || isPointerToConcrete(T) {
36 for i, n := 0, mset.Len(); i < n; i++ {
37 result = append(result, mset.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.MethodSet(types.NewPointer(T))
43 for i, n := 0, pmset.Len(); i < n; i++ {
44 meth := pmset.At(i)
45 if m := mset.Lookup(meth.Obj().Pkg(), meth.Obj().Name()); m != nil {
46 meth = m
47 }
48 result = append(result, meth)
49 }
50
51 }
52 return result
53 }