GoPLS Viewer

Home|gopls/go/types/typeutil/ui.go
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
5package typeutil
6
7// This file defines utilities for user interfaces that display types.
8
9import "go/types"
10
11// IntuitiveMethodSet returns the intuitive method set of a type T,
12// which is the set of methods you can call on an addressable value of
13// that type.
14//
15// The result always contains MethodSet(T), and is exactly MethodSet(T)
16// for interface types and for pointer-to-concrete types.
17// For all other concrete types T, the result additionally
18// contains each method belonging to *T if there is no identically
19// named method on T itself.
20//
21// This corresponds to user intuition about method sets;
22// this function is intended only for user interfaces.
23//
24// The order of the result is as for types.MethodSet(T).
25func IntuitiveMethodSet(T types.Typemsets *MethodSetCache) []*types.Selection {
26    isPointerToConcrete := func(T types.Typebool {
27        ptrok := T.(*types.Pointer)
28        return ok && !types.IsInterface(ptr.Elem())
29    }
30
31    var result []*types.Selection
32    mset := msets.MethodSet(T)
33    if types.IsInterface(T) || isPointerToConcrete(T) {
34        for in := 0mset.Len(); i < ni++ {
35            result = append(resultmset.At(i))
36        }
37    } else {
38        // T is some other concrete type.
39        // Report methods of T and *T, preferring those of T.
40        pmset := msets.MethodSet(types.NewPointer(T))
41        for in := 0pmset.Len(); i < ni++ {
42            meth := pmset.At(i)
43            if m := mset.Lookup(meth.Obj().Pkg(), meth.Obj().Name()); m != nil {
44                meth = m
45            }
46            result = append(resultmeth)
47        }
48
49    }
50    return result
51}
52
MembersX
IntuitiveMethodSet
IntuitiveMethodSet.T
IntuitiveMethodSet.result
IntuitiveMethodSet.mset
IntuitiveMethodSet.BlockStmt.i
IntuitiveMethodSet.BlockStmt.n
IntuitiveMethodSet.BlockStmt.pmset
IntuitiveMethodSet.msets
IntuitiveMethodSet.BlockStmt.BlockStmt.meth
IntuitiveMethodSet.BlockStmt.BlockStmt.m
Members
X