CFuncPtr.Hash

This commit is contained in:
xushiwei
2024-05-03 18:02:09 +08:00
parent 13a1c8ac4b
commit 236debab33
2 changed files with 49 additions and 36 deletions

View File

@@ -4,7 +4,7 @@
// Package typeutil defines various utilities for types, such as Map,
// a mapping from types.Type to interface{} values.
package typeutil // import "golang.org/x/tools/go/types/typeutil"
package typeutil
import (
"bytes"
@@ -253,39 +253,7 @@ func hashString(s string) uint32 {
return h
}
// hashFor computes the hash of t.
func (h Hasher) hashFor(t types.Type) uint32 {
// See Identical for rationale.
switch t := t.(type) {
case *types.Basic:
return uint32(t.Kind())
case *aliases.Alias:
return h.Hash(t.Underlying())
case *types.Array:
return 9043 + 2*uint32(t.Len()) + 3*h.Hash(t.Elem())
case *types.Slice:
return 9049 + 2*h.Hash(t.Elem())
case *types.Struct:
var hash uint32 = 9059
for i, n := 0, t.NumFields(); i < n; i++ {
f := t.Field(i)
if f.Anonymous() {
hash += 8861
}
hash += hashString(t.Tag(i))
hash += hashString(f.Name()) // (ignore f.Pkg)
hash += h.Hash(f.Type())
}
return hash
case *types.Pointer:
return 9067 + 2*h.Hash(t.Elem())
case *types.Signature:
func HashSig(h Hasher, t *types.Signature) uint32 {
var hash uint32 = 9091
if t.Variadic() {
hash *= 8863
@@ -321,6 +289,42 @@ func (h Hasher) hashFor(t types.Type) uint32 {
}
return hash + 3*h.hashTuple(t.Params()) + 5*h.hashTuple(t.Results())
}
// hashFor computes the hash of t.
func (h Hasher) hashFor(t types.Type) uint32 {
// See Identical for rationale.
switch t := t.(type) {
case *types.Basic:
return uint32(t.Kind())
case *aliases.Alias:
return h.Hash(t.Underlying())
case *types.Array:
return 9043 + 2*uint32(t.Len()) + 3*h.Hash(t.Elem())
case *types.Slice:
return 9049 + 2*h.Hash(t.Elem())
case *types.Struct:
var hash uint32 = 9059
for i, n := 0, t.NumFields(); i < n; i++ {
f := t.Field(i)
if f.Anonymous() {
hash += 8861
}
hash += hashString(t.Tag(i))
hash += hashString(f.Name()) // (ignore f.Pkg)
hash += h.Hash(f.Type())
}
return hash
case *types.Pointer:
return 9067 + 2*h.Hash(t.Elem())
case *types.Signature:
return HashSig(h, t)
case *types.Union:
return h.hashUnion(t)
@@ -370,6 +374,9 @@ func (h Hasher) hashFor(t types.Type) uint32 {
case *types.Tuple:
return h.hashTuple(t)
case interface{ Hash(h Hasher) uint32 }:
return t.Hash(h)
}
panic(fmt.Sprintf("%T: %v", t, t))

View File

@@ -18,6 +18,8 @@ package ssa
import (
"go/types"
"github.com/goplus/llgo/internal/typeutil"
)
// -----------------------------------------------------------------------------
@@ -46,6 +48,10 @@ type CFuncPtr types.Signature
func (t *CFuncPtr) String() string { return (*types.Signature)(t).String() }
func (t *CFuncPtr) Underlying() types.Type { return (*types.Signature)(t) }
func (t *CFuncPtr) Hash(h typeutil.Hasher) uint32 {
return typeutil.HashSig(h, (*types.Signature)(t))*13 + 97
}
// -----------------------------------------------------------------------------
// CType convert a C type into Go.