diff options
author | Russ Cox <rsc@golang.org> | 2020-12-23 00:08:03 -0500 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2020-12-23 06:38:01 +0000 |
commit | 9ee309255a94499c6f4e6d3ac7653b5eeb4ae7b7 (patch) | |
tree | 3a166c8ebba0ca3a374fb126fd58e7b74758ed3a /src/cmd/compile/internal/types/type.go | |
parent | ead4957892bc1975d9cc9c32777733c67e5a885e (diff) | |
download | go-9ee309255a94499c6f4e6d3ac7653b5eeb4ae7b7.tar.gz go-9ee309255a94499c6f4e6d3ac7653b5eeb4ae7b7.zip |
[dev.regabi] cmd/compile: move helpers into package types [generated]
[git-generate]
cd src/cmd/compile/internal/gc
rf '
# Type hash (formatting).
mv typehash TypeHash
mv TypeHash fmt.go
# Method sorting.
mv methcmp MethodsByName
mv MethodsByName MethodsByName.Len MethodsByName.Swap \
MethodsByName.Less sort.go
# Move version check into types.
# A little surprising, but its keyed off the types.Pkg.
ex {
import "cmd/compile/internal/types"
var p *types.Pkg
var major, minor int
langSupported(major, minor, p) -> AllowsGoVersion(p, major, minor)
}
rm langSupported
mv checkLang ParseLangFlag
mv lang langWant AllowsGoVersion ParseLangFlag \
parseLang currentLang goVersionRE goversion.go
mv testdclstack CheckDclstack
mv CheckDclstack scope.go
mv algtype1 AlgType
mv isComplex IsComplex
mv isFloat IsFloat
mv isInt IsInt
mv issimple IsSimple
mv okforcmp IsOrdered
mv floatForComplex FloatForComplex
mv complexForFloat ComplexForFloat
mv isdirectiface IsDirectIface
mv isifacemethod IsInterfaceMethod
mv isMethodApplicable IsMethodApplicable
mv ispaddedfield IsPaddedField
mv isRuntimePkg IsRuntimePkg
mv isReflectPkg IsReflectPkg
mv methtype ReceiverBaseType
mv typesymname TypeSymName
mv typesym TypeSym
mv typeLookup TypeSymLookup
mv IsAlias IsDotAlias
mv isreflexive IsReflexive
mv simtype SimType
# The type1.go here is to avoid an undiagnosed bug in rf
# that does not get the follow-up typechecking right if we
# move directly to type.go during the mv into package types below.
mv \
IsInt IsOrdered IsReflexive \
IsDirectIface IsInterfaceMethod IsMethodApplicable IsPaddedField \
IsRuntimePkg IsReflectPkg ReceiverBaseType \
FloatForComplex ComplexForFloat \
TypeSym TypeSymLookup TypeSymName \
typepkg SimType \
type1.go
# The alg1.go here is because we are only moving part of alg.go.
mv typeHasNoAlg TypeHasNoAlg
mv AlgKind ANOEQ AlgType TypeHasNoAlg IsComparable IncomparableField IsPaddedField alg1.go
mv IsDotAlias pkg.go
mv alg1.go algkind_string.go fmt.go goversion.go pkg.go \
CheckDclstack \ # scope.go
sort.go type1.go \
cmd/compile/internal/types
'
cd ../types
rf '
mv IsDclstackValid isDclstackValid
mv alg1.go alg.go
mv type1.go type.go
'
Change-Id: I8bd53b21c7bdd1770e1b525de32f136833e84c9d
Reviewed-on: https://go-review.googlesource.com/c/go/+/279307
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/cmd/compile/internal/types/type.go')
-rw-r--r-- | src/cmd/compile/internal/types/type.go | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/types/type.go b/src/cmd/compile/internal/types/type.go index 752c268fa2..21d96c430a 100644 --- a/src/cmd/compile/internal/types/type.go +++ b/src/cmd/compile/internal/types/type.go @@ -9,6 +9,7 @@ import ( "cmd/internal/obj" "cmd/internal/src" "fmt" + "sync" ) // IRNode represents an ir.Node, but without needing to import cmd/compile/internal/ir, @@ -1695,3 +1696,204 @@ func anyBroke(fields []*Field) bool { } return false } + +var ( + IsInt [NTYPE]bool + IsFloat [NTYPE]bool + IsComplex [NTYPE]bool + IsSimple [NTYPE]bool +) + +var IsOrdered [NTYPE]bool + +// IsReflexive reports whether t has a reflexive equality operator. +// That is, if x==x for all x of type t. +func IsReflexive(t *Type) bool { + switch t.Kind() { + case TBOOL, + TINT, + TUINT, + TINT8, + TUINT8, + TINT16, + TUINT16, + TINT32, + TUINT32, + TINT64, + TUINT64, + TUINTPTR, + TPTR, + TUNSAFEPTR, + TSTRING, + TCHAN: + return true + + case TFLOAT32, + TFLOAT64, + TCOMPLEX64, + TCOMPLEX128, + TINTER: + return false + + case TARRAY: + return IsReflexive(t.Elem()) + + case TSTRUCT: + for _, t1 := range t.Fields().Slice() { + if !IsReflexive(t1.Type) { + return false + } + } + return true + + default: + base.Fatalf("bad type for map key: %v", t) + return false + } +} + +// Can this type be stored directly in an interface word? +// Yes, if the representation is a single pointer. +func IsDirectIface(t *Type) bool { + if t.Broke() { + return false + } + + switch t.Kind() { + case TPTR: + // Pointers to notinheap types must be stored indirectly. See issue 42076. + return !t.Elem().NotInHeap() + case TCHAN, + TMAP, + TFUNC, + TUNSAFEPTR: + return true + + case TARRAY: + // Array of 1 direct iface type can be direct. + return t.NumElem() == 1 && IsDirectIface(t.Elem()) + + case TSTRUCT: + // Struct with 1 field of direct iface type can be direct. + return t.NumFields() == 1 && IsDirectIface(t.Field(0).Type) + } + + return false +} + +// IsInterfaceMethod reports whether (field) m is +// an interface method. Such methods have the +// special receiver type types.FakeRecvType(). +func IsInterfaceMethod(f *Type) bool { + return f.Recv().Type == FakeRecvType() +} + +// IsMethodApplicable reports whether method m can be called on a +// value of type t. This is necessary because we compute a single +// method set for both T and *T, but some *T methods are not +// applicable to T receivers. +func IsMethodApplicable(t *Type, m *Field) bool { + return t.IsPtr() || !m.Type.Recv().Type.IsPtr() || IsInterfaceMethod(m.Type) || m.Embedded == 2 +} + +// IsRuntimePkg reports whether p is package runtime. +func IsRuntimePkg(p *Pkg) bool { + if base.Flag.CompilingRuntime && p == LocalPkg { + return true + } + return p.Path == "runtime" +} + +// IsReflectPkg reports whether p is package reflect. +func IsReflectPkg(p *Pkg) bool { + if p == LocalPkg { + return base.Ctxt.Pkgpath == "reflect" + } + return p.Path == "reflect" +} + +// ReceiverBaseType returns the underlying type, if any, +// that owns methods with receiver parameter t. +// The result is either a named type or an anonymous struct. +func ReceiverBaseType(t *Type) *Type { + if t == nil { + return nil + } + + // Strip away pointer if it's there. + if t.IsPtr() { + if t.Sym() != nil { + return nil + } + t = t.Elem() + if t == nil { + return nil + } + } + + // Must be a named type or anonymous struct. + if t.Sym() == nil && !t.IsStruct() { + return nil + } + + // Check types. + if IsSimple[t.Kind()] { + return t + } + switch t.Kind() { + case TARRAY, TCHAN, TFUNC, TMAP, TSLICE, TSTRING, TSTRUCT: + return t + } + return nil +} + +func FloatForComplex(t *Type) *Type { + switch t.Kind() { + case TCOMPLEX64: + return Types[TFLOAT32] + case TCOMPLEX128: + return Types[TFLOAT64] + } + base.Fatalf("unexpected type: %v", t) + return nil +} + +func ComplexForFloat(t *Type) *Type { + switch t.Kind() { + case TFLOAT32: + return Types[TCOMPLEX64] + case TFLOAT64: + return Types[TCOMPLEX128] + } + base.Fatalf("unexpected type: %v", t) + return nil +} + +func TypeSym(t *Type) *Sym { + return TypeSymLookup(TypeSymName(t)) +} + +func TypeSymLookup(name string) *Sym { + typepkgmu.Lock() + s := typepkg.Lookup(name) + typepkgmu.Unlock() + return s +} + +func TypeSymName(t *Type) string { + name := t.ShortString() + // Use a separate symbol name for Noalg types for #17752. + if TypeHasNoAlg(t) { + name = "noalg." + name + } + return name +} + +// Fake package for runtime type info (headers) +// Don't access directly, use typeLookup below. +var ( + typepkgmu sync.Mutex // protects typepkg lookups + typepkg = NewPkg("type", "type") +) + +var SimType [NTYPE]Kind |