1 // Copyright 2024 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 typesinternal 6 7 import ( 8 "go/types" 9 10 "golang.org/x/tools/internal/aliases" 11 ) 12 13 // ReceiverNamed returns the named type (if any) associated with the 14 // type of recv, which may be of the form N or *N, or aliases thereof. 15 // It also reports whether a Pointer was present. 16 func ReceiverNamed(recv *types.Var) (isPtr bool, named *types.Named) { 17 t := recv.Type() 18 if ptr, ok := aliases.Unalias(t).(*types.Pointer); ok { 19 isPtr = true 20 t = ptr.Elem() 21 } 22 named, _ = aliases.Unalias(t).(*types.Named) 23 return 24 } 25 26 // Unpointer returns T given *T or an alias thereof. 27 // For all other types it is the identity function. 28 // It does not look at underlying types. 29 // The result may be an alias. 30 // 31 // Use this function to strip off the optional pointer on a receiver 32 // in a field or method selection, without losing the named type 33 // (which is needed to compute the method set). 34 // 35 // See also [typeparams.MustDeref], which removes one level of 36 // indirection from the type, regardless of named types (analogous to 37 // a LOAD instruction). 38 func Unpointer(t types.Type) types.Type { 39 if ptr, ok := aliases.Unalias(t).(*types.Pointer); ok { 40 return ptr.Elem() 41 } 42 return t 43 } 44