CFuncPtr.Hash
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
// Package typeutil defines various utilities for types, such as Map,
|
// Package typeutil defines various utilities for types, such as Map,
|
||||||
// a mapping from types.Type to interface{} values.
|
// a mapping from types.Type to interface{} values.
|
||||||
package typeutil // import "golang.org/x/tools/go/types/typeutil"
|
package typeutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -253,39 +253,7 @@ func hashString(s string) uint32 {
|
|||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
// hashFor computes the hash of t.
|
func HashSig(h Hasher, t *types.Signature) uint32 {
|
||||||
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:
|
|
||||||
var hash uint32 = 9091
|
var hash uint32 = 9091
|
||||||
if t.Variadic() {
|
if t.Variadic() {
|
||||||
hash *= 8863
|
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())
|
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:
|
case *types.Union:
|
||||||
return h.hashUnion(t)
|
return h.hashUnion(t)
|
||||||
@@ -370,6 +374,9 @@ func (h Hasher) hashFor(t types.Type) uint32 {
|
|||||||
|
|
||||||
case *types.Tuple:
|
case *types.Tuple:
|
||||||
return h.hashTuple(t)
|
return h.hashTuple(t)
|
||||||
|
|
||||||
|
case interface{ Hash(h Hasher) uint32 }:
|
||||||
|
return t.Hash(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
panic(fmt.Sprintf("%T: %v", t, t))
|
panic(fmt.Sprintf("%T: %v", t, t))
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ package ssa
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"go/types"
|
"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) String() string { return (*types.Signature)(t).String() }
|
||||||
func (t *CFuncPtr) Underlying() types.Type { return (*types.Signature)(t) }
|
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.
|
// CType convert a C type into Go.
|
||||||
|
|||||||
Reference in New Issue
Block a user