aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/typecheck/universe.go
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2021-08-26 13:17:56 -0700
committerMatthew Dempsky <mdempsky@google.com>2021-08-26 22:37:54 +0000
commit967a8017f7d41717335ee020e46e2c55c5c9e96e (patch)
treeb1ec12b24cfd28630301df4d6a487818d8e71af0 /src/cmd/compile/internal/typecheck/universe.go
parentaf80af22b507ae23ae04372f30b98f7720c85f8a (diff)
downloadgo-967a8017f7d41717335ee020e46e2c55c5c9e96e.tar.gz
go-967a8017f7d41717335ee020e46e2c55c5c9e96e.zip
cmd/compile: move types init code into package types
This moves the package types setup code from package typecheck into package types itself. This is a prereq for making types.Type more opaque, because some unit tests depend on being able to init the basic universal types. A few notable details of this CL: 1. Creating the builtin types requires being able to create the ir.Name/ir.OTYPE that represents it, but package types can't depend on package ir. So we add a callback function to handle creating the ir.Name. 2. This CL moves ir.Pkgs.Unsafe to types.UnsafePkg. Package unsafe is part of the language, not like the other ir.Pkgs packages that are purely implementation details. 3. This CL also moves typecheck.FakeRecv to types.FakeRecv, addressing an outstanding TODO. Change-Id: I64de04ce82fbcd1bb59f547e2eea3cda52d89429 Reviewed-on: https://go-review.googlesource.com/c/go/+/345474 Trust: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/typecheck/universe.go')
-rw-r--r--src/cmd/compile/internal/typecheck/universe.go144
1 files changed, 6 insertions, 138 deletions
diff --git a/src/cmd/compile/internal/typecheck/universe.go b/src/cmd/compile/internal/typecheck/universe.go
index a7c84dc8d8..ebe338e2aa 100644
--- a/src/cmd/compile/internal/typecheck/universe.go
+++ b/src/cmd/compile/internal/typecheck/universe.go
@@ -29,37 +29,6 @@ var (
okforarith [types.NTYPE]bool
)
-var basicTypes = [...]struct {
- name string
- etype types.Kind
-}{
- {"int8", types.TINT8},
- {"int16", types.TINT16},
- {"int32", types.TINT32},
- {"int64", types.TINT64},
- {"uint8", types.TUINT8},
- {"uint16", types.TUINT16},
- {"uint32", types.TUINT32},
- {"uint64", types.TUINT64},
- {"float32", types.TFLOAT32},
- {"float64", types.TFLOAT64},
- {"complex64", types.TCOMPLEX64},
- {"complex128", types.TCOMPLEX128},
- {"bool", types.TBOOL},
- {"string", types.TSTRING},
-}
-
-var typedefs = [...]struct {
- name string
- etype types.Kind
- sameas32 types.Kind
- sameas64 types.Kind
-}{
- {"int", types.TINT, types.TINT32, types.TINT64},
- {"uint", types.TUINT, types.TUINT32, types.TUINT64},
- {"uintptr", types.TUINTPTR, types.TUINT32, types.TUINT64},
-}
-
var builtinFuncs = [...]struct {
name string
op ir.Op
@@ -94,86 +63,12 @@ var unsafeFuncs = [...]struct {
// InitUniverse initializes the universe block.
func InitUniverse() {
- if types.PtrSize == 0 {
- base.Fatalf("typeinit before betypeinit")
- }
-
- types.SlicePtrOffset = 0
- types.SliceLenOffset = types.Rnd(types.SlicePtrOffset+int64(types.PtrSize), int64(types.PtrSize))
- types.SliceCapOffset = types.Rnd(types.SliceLenOffset+int64(types.PtrSize), int64(types.PtrSize))
- types.SliceSize = types.Rnd(types.SliceCapOffset+int64(types.PtrSize), int64(types.PtrSize))
-
- // string is same as slice wo the cap
- types.StringSize = types.Rnd(types.SliceLenOffset+int64(types.PtrSize), int64(types.PtrSize))
-
- for et := types.Kind(0); et < types.NTYPE; et++ {
- types.SimType[et] = et
- }
-
- types.Types[types.TANY] = types.New(types.TANY)
- types.Types[types.TINTER] = types.NewInterface(types.LocalPkg, nil)
-
- defBasic := func(kind types.Kind, pkg *types.Pkg, name string) *types.Type {
- sym := pkg.Lookup(name)
+ types.InitTypes(func(sym *types.Sym, typ *types.Type) types.Object {
n := ir.NewDeclNameAt(src.NoXPos, ir.OTYPE, sym)
- t := types.NewBasic(kind, n)
- n.SetType(t)
+ n.SetType(typ)
sym.Def = n
- if kind != types.TANY {
- types.CalcSize(t)
- }
- return t
- }
-
- for _, s := range &basicTypes {
- types.Types[s.etype] = defBasic(s.etype, types.BuiltinPkg, s.name)
- }
-
- for _, s := range &typedefs {
- sameas := s.sameas32
- if types.PtrSize == 8 {
- sameas = s.sameas64
- }
- types.SimType[s.etype] = sameas
-
- types.Types[s.etype] = defBasic(s.etype, types.BuiltinPkg, s.name)
- }
-
- // We create separate byte and rune types for better error messages
- // rather than just creating type alias *types.Sym's for the uint8 and
- // int32 types. Hence, (bytetype|runtype).Sym.isAlias() is false.
- // TODO(gri) Should we get rid of this special case (at the cost
- // of less informative error messages involving bytes and runes)?
- // (Alternatively, we could introduce an OTALIAS node representing
- // type aliases, albeit at the cost of having to deal with it everywhere).
- types.ByteType = defBasic(types.TUINT8, types.BuiltinPkg, "byte")
- types.RuneType = defBasic(types.TINT32, types.BuiltinPkg, "rune")
-
- // error type
- s := types.BuiltinPkg.Lookup("error")
- n := ir.NewDeclNameAt(src.NoXPos, ir.OTYPE, s)
- types.ErrorType = types.NewNamed(n)
- types.ErrorType.SetUnderlying(makeErrorInterface())
- n.SetType(types.ErrorType)
- s.Def = n
- types.CalcSize(types.ErrorType)
-
- // comparable type (interface)
- s = types.BuiltinPkg.Lookup("comparable")
- n = ir.NewDeclNameAt(src.NoXPos, ir.OTYPE, s)
- types.ComparableType = types.NewNamed(n)
- types.ComparableType.SetUnderlying(makeComparableInterface())
- n.SetType(types.ComparableType)
- s.Def = n
- types.CalcSize(types.ComparableType)
-
- types.Types[types.TUNSAFEPTR] = defBasic(types.TUNSAFEPTR, ir.Pkgs.Unsafe, "Pointer")
-
- // simple aliases
- types.SimType[types.TMAP] = types.TPTR
- types.SimType[types.TCHAN] = types.TPTR
- types.SimType[types.TFUNC] = types.TPTR
- types.SimType[types.TUNSAFEPTR] = types.TPTR
+ return n
+ })
for _, s := range &builtinFuncs {
s2 := types.BuiltinPkg.Lookup(s.name)
@@ -183,13 +78,13 @@ func InitUniverse() {
}
for _, s := range &unsafeFuncs {
- s2 := ir.Pkgs.Unsafe.Lookup(s.name)
+ s2 := types.UnsafePkg.Lookup(s.name)
def := NewName(s2)
def.BuiltinOp = s.op
s2.Def = def
}
- s = types.BuiltinPkg.Lookup("true")
+ s := types.BuiltinPkg.Lookup("true")
s.Def = ir.NewConstAt(src.NoXPos, s, types.UntypedBool, constant.MakeBool(true))
s = types.BuiltinPkg.Lookup("false")
@@ -219,19 +114,6 @@ func InitUniverse() {
s = types.BuiltinPkg.Lookup("iota")
s.Def = ir.NewIota(base.Pos, s)
- for et := types.TINT8; et <= types.TUINT64; et++ {
- types.IsInt[et] = true
- }
- types.IsInt[types.TINT] = true
- types.IsInt[types.TUINT] = true
- types.IsInt[types.TUINTPTR] = true
-
- types.IsFloat[types.TFLOAT32] = true
- types.IsFloat[types.TFLOAT64] = true
-
- types.IsComplex[types.TCOMPLEX64] = true
- types.IsComplex[types.TCOMPLEX128] = true
-
// initialize okfor
for et := types.Kind(0); et < types.NTYPE; et++ {
if types.IsInt[et] || et == types.TIDEAL {
@@ -331,20 +213,6 @@ func InitUniverse() {
okfor[ir.OLEN] = okforlen[:]
}
-func makeErrorInterface() *types.Type {
- sig := types.NewSignature(types.NoPkg, fakeRecvField(), nil, nil, []*types.Field{
- types.NewField(src.NoXPos, nil, types.Types[types.TSTRING]),
- })
- method := types.NewField(src.NoXPos, Lookup("Error"), sig)
- return types.NewInterface(types.NoPkg, []*types.Field{method})
-}
-
-func makeComparableInterface() *types.Type {
- sig := types.NewSignature(types.NoPkg, fakeRecvField(), nil, nil, nil)
- method := types.NewField(src.NoXPos, Lookup("=="), sig)
- return types.NewInterface(types.NoPkg, []*types.Field{method})
-}
-
// DeclareUniverse makes the universe block visible within the current package.
func DeclareUniverse() {
// Operationally, this is similar to a dot import of builtinpkg, except