tools: golang.org/x/tools/go/types/typeutil Index | Examples | Files

package typeutil

import "golang.org/x/tools/go/types/typeutil"

Package typeutil defines various utilities for types, such as Map, a mapping from types.Type to interface{} values.

Index

Examples

Package Files

imports.go map.go methodsetcache.go ui.go

func Dependencies Uses

func Dependencies(pkgs ...*types.Package) []*types.Package

Dependencies returns all dependencies of the specified packages.

Dependent packages appear in topological order: if package P imports package Q, Q appears earlier than P in the result. The algorithm follows import statements in the order they appear in the source code, so the result is a total order.

func IntuitiveMethodSet Uses

func IntuitiveMethodSet(T types.Type, msets *MethodSetCache) []*types.Selection

IntuitiveMethodSet returns the intuitive method set of a type T, which is the set of methods you can call on an addressable value of that type.

The result always contains MethodSet(T), and is exactly MethodSet(T) for interface types and for pointer-to-concrete types. For all other concrete types T, the result additionally contains each method belonging to *T if there is no identically named method on T itself.

This corresponds to user intuition about method sets; this function is intended only for user interfaces.

The order of the result is as for types.MethodSet(T).

type Hasher Uses

type Hasher struct {
    // contains filtered or unexported fields
}

A Hasher maps each type to its hash value. For efficiency, a hasher uses memoization; thus its memory footprint grows monotonically over time. Hashers are not thread-safe. Hashers have reference semantics. Call MakeHasher to create a Hasher.

func MakeHasher Uses

func MakeHasher() Hasher

MakeHasher returns a new Hasher instance.

func (Hasher) Hash Uses

func (h Hasher) Hash(t types.Type) uint32

Hash computes a hash value for the given type t such that Identical(t, t') => Hash(t) == Hash(t').

type Map Uses

type Map struct {
    // contains filtered or unexported fields
}

Map is a hash-table-based mapping from types (types.Type) to arbitrary interface{} values. The concrete types that implement the Type interface are pointers. Since they are not canonicalized, == cannot be used to check for equivalence, and thus we cannot simply use a Go map.

Just as with map[K]V, a nil *Map is a valid empty map.

Not thread-safe.

Code:

const source = `package P

var X []string
var Y []string

const p, q = 1.0, 2.0

func f(offset int32) (value byte, ok bool)
func g(rune) (uint8, bool)
`

// Parse and type-check the package.
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "P.go", source, 0)
if err != nil {
    panic(err)
}
pkg, err := new(types.Config).Check("P", fset, []*ast.File{f}, nil)
if err != nil {
    panic(err)
}

scope := pkg.Scope()

// Group names of package-level objects by their type.
var namesByType typeutil.Map // value is []string
for _, name := range scope.Names() {
    T := scope.Lookup(name).Type()

    names, _ := namesByType.At(T).([]string)
    names = append(names, name)
    namesByType.Set(T, names)
}

// Format, sort, and print the map entries.
var lines []string
namesByType.Iterate(func(T types.Type, names interface{}) {
    lines = append(lines, fmt.Sprintf("%s   %s", names, T))
})
sort.Strings(lines)
for _, line := range lines {
    fmt.Println(line)
}

Output:

[X Y]   []string
[f g]   func(offset int32) (value byte, ok bool)
[p q]   untyped float

func (*Map) At Uses

func (m *Map) At(key types.Type) interface{}

At returns the map entry for the given key. The result is nil if the entry is not present.

func (*Map) Delete Uses

func (m *Map) Delete(key types.Type) bool

Delete removes the entry with the given key, if any. It returns true if the entry was found.

func (*Map) Iterate Uses

func (m *Map) Iterate(f func(key types.Type, value interface{}))

Iterate calls function f on each entry in the map in unspecified order.

If f should mutate the map, Iterate provides the same guarantees as Go maps: if f deletes a map entry that Iterate has not yet reached, f will not be invoked for it, but if f inserts a map entry that Iterate has not yet reached, whether or not f will be invoked for it is unspecified.

func (*Map) Keys Uses

func (m *Map) Keys() []types.Type

Keys returns a new slice containing the set of map keys. The order is unspecified.

func (*Map) KeysString Uses

func (m *Map) KeysString() string

KeysString returns a string representation of the map's key set. Order is unspecified.

func (*Map) Len Uses

func (m *Map) Len() int

Len returns the number of map entries.

func (*Map) Set Uses

func (m *Map) Set(key types.Type, value interface{}) (prev interface{})

Set sets the map entry for key to val, and returns the previous entry, if any.

func (*Map) SetHasher Uses

func (m *Map) SetHasher(hasher Hasher)

SetHasher sets the hasher used by Map.

All Hashers are functionally equivalent but contain internal state used to cache the results of hashing previously seen types.

A single Hasher created by MakeHasher() may be shared among many Maps. This is recommended if the instances have many keys in common, as it will amortize the cost of hash computation.

A Hasher may grow without bound as new types are seen. Even when a type is deleted from the map, the Hasher never shrinks, since other types in the map may reference the deleted type indirectly.

Hashers are not thread-safe, and read-only operations such as Map.Lookup require updates to the hasher, so a full Mutex lock (not a read-lock) is require around all Map operations if a shared hasher is accessed from multiple threads.

If SetHasher is not called, the Map will create a private hasher at the first call to Insert.

func (*Map) String Uses

func (m *Map) String() string

String returns a string representation of the map's entries. Values are printed using fmt.Sprintf("%v", v). Order is unspecified.

type MethodSetCache Uses

type MethodSetCache struct {
    // contains filtered or unexported fields
}

A MethodSetCache records the method set of each type T for which MethodSet(T) is called so that repeat queries are fast. The zero value is a ready-to-use cache instance.

func (*MethodSetCache) MethodSet Uses

func (cache *MethodSetCache) MethodSet(T types.Type) *types.MethodSet

MethodSet returns the method set of type T. It is thread-safe.

If cache is nil, this function is equivalent to types.NewMethodSet(T). Utility functions can thus expose an optional *MethodSetCache parameter to clients that care about performance.

Package typeutil imports 5 packages (graph) and is imported by 43 packages. Updated 2017-07-18. Refresh now. Tools for package owners.