| 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 | // This file implements a cache of method sets. |
| 6 | |
| 7 | package typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/ |
| 8 | |
| 9 | import ( |
| 10 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 11 | "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/" |
| 12 | ) |
| 13 | |
| 14 | // A MethodSetCache records the method set of each type T for which |
| 15 | // MethodSet(T) is called so that repeat queries are fast. |
| 16 | // The zero value is a ready-to-use cache instance. |
| 17 | type MethodSetCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache# struct { |
| 18 | muscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#mu. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex# |
| 19 | namedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#named. map[*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#]struct{ valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#$anon_cbf73c5105837cf5#value., pointerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#$anon_cbf73c5105837cf5#pointer. *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.MethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet# } // method sets for named N and *N |
| 20 | othersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#others. map[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#]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.MethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet# // all other types |
| 21 | } |
| 22 | |
| 23 | // MethodSet returns the method set of type T. It is thread-safe. |
| 24 | // |
| 25 | // If cache is nil, this function is equivalent to types.NewMethodSet(T). |
| 26 | // Utility functions can thus expose an optional *MethodSetCache |
| 27 | // parameter to clients that care about performance. |
| 28 | func (cache *MethodSetCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#) MethodSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#MethodSet().(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#) *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.MethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet# { |
| 29 | if cache == nil { |
| 30 | return typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewMethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewMethodSet().(T) |
| 31 | } |
| 32 | cache.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 33 | defer cache.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 34 | |
| 35 | switch T := 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).(type) { |
| 36 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#: |
| 37 | return cache.lookupNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#lookupNamed().(T).value |
| 38 | |
| 39 | case *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#: |
| 40 | if N, 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.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#Elem().()).(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#); ok { |
| 41 | return cache.lookupNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#lookupNamed().(N).pointer |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // all other types |
| 46 | // (The map uses pointer equivalence, not type identity.) |
| 47 | mset := cache.othersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#others.[T] |
| 48 | if mset == nil { |
| 49 | mset = typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewMethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewMethodSet().(T) |
| 50 | if cache.othersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#others. == nil { |
| 51 | cache.othersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#others. = make(map[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#]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.MethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet#) |
| 52 | } |
| 53 | cache.othersscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#others.[T] = mset |
| 54 | } |
| 55 | return mset |
| 56 | } |
| 57 | |
| 58 | func (cache *MethodSetCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#) lookupNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#lookupNamed().(named *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#) struct{ value, pointer *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.MethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet# } { |
| 59 | if cache.namedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#named. == nil { |
| 60 | cache.namedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#named. = make(map[*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#]struct{ value, pointer *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.MethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/MethodSet# }) |
| 61 | } |
| 62 | // Avoid recomputing mset(*T) for each distinct Pointer |
| 63 | // instance whose underlying type is a named type. |
| 64 | msets, ok := cache.namedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#named.[named] |
| 65 | if !ok { |
| 66 | msets.valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#$anon_cbf73c5105837cf5#value. = typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewMethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewMethodSet().(named) |
| 67 | msets.pointerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#$anon_cbf73c5105837cf5#pointer. = typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewMethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewMethodSet().(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().(named)) |
| 68 | cache.namedscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#named.[named] = msets |
| 69 | } |
| 70 | return msets |
| 71 | } |