mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
[dev.regabi] cmd/compile: add Pkg parameter to type constructors
Allows getting rid of the SetPkg method and also addresses a long-standing TODO in the exporter. Suggested by rsc@. Passes buildall w/ toolstash -cmp. Change-Id: Ib294f75f1350572efb2e0d993d49efef884de3d4 Reviewed-on: https://go-review.googlesource.com/c/go/+/274440 Trust: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
parent
42e46f4ae0
commit
c10b0ad628
8 changed files with 32 additions and 47 deletions
|
|
@ -464,8 +464,6 @@ func makepartialcall(dot ir.Node, t0 *types.Type, meth *types.Sym) *ir.Func {
|
||||||
fn.SetDupok(true)
|
fn.SetDupok(true)
|
||||||
fn.SetNeedctxt(true)
|
fn.SetNeedctxt(true)
|
||||||
|
|
||||||
tfn.Type().SetPkg(t0.Pkg())
|
|
||||||
|
|
||||||
// Declare and initialize variable holding receiver.
|
// Declare and initialize variable holding receiver.
|
||||||
cr := ir.NewClosureRead(rcvrtype, Rnd(int64(Widthptr), int64(rcvrtype.Align)))
|
cr := ir.NewClosureRead(rcvrtype, Rnd(int64(Widthptr), int64(rcvrtype.Align)))
|
||||||
ptr := NewName(lookup(".this"))
|
ptr := NewName(lookup(".this"))
|
||||||
|
|
|
||||||
|
|
@ -552,7 +552,7 @@ func tostruct(l []*ir.Field) *types.Type {
|
||||||
checkdupfields("field", fields)
|
checkdupfields("field", fields)
|
||||||
|
|
||||||
base.Pos = lno
|
base.Pos = lno
|
||||||
return types.NewStruct(fields)
|
return types.NewStruct(ir.LocalPkg, fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tointerface(nmethods []*ir.Field) *types.Type {
|
func tointerface(nmethods []*ir.Field) *types.Type {
|
||||||
|
|
@ -573,7 +573,7 @@ func tointerface(nmethods []*ir.Field) *types.Type {
|
||||||
}
|
}
|
||||||
|
|
||||||
base.Pos = lno
|
base.Pos = lno
|
||||||
return types.NewInterface(methods)
|
return types.NewInterface(ir.LocalPkg, methods)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fakeRecv() *ir.Field {
|
func fakeRecv() *ir.Field {
|
||||||
|
|
@ -625,7 +625,7 @@ func functype(nrecv *ir.Field, nparams, nresults []*ir.Field) *types.Type {
|
||||||
recv = funarg(nrecv)
|
recv = funarg(nrecv)
|
||||||
}
|
}
|
||||||
|
|
||||||
t := types.NewSignature(recv, funargs(nparams), funargs(nresults))
|
t := types.NewSignature(ir.LocalPkg, recv, funargs(nparams), funargs(nresults))
|
||||||
checkdupfields("argument", t.Recvs().FieldSlice(), t.Params().FieldSlice(), t.Results().FieldSlice())
|
checkdupfields("argument", t.Recvs().FieldSlice(), t.Params().FieldSlice(), t.Results().FieldSlice())
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -718,10 +718,8 @@ func (w *exportWriter) doTyp(t *types.Type) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *exportWriter) setPkg(pkg *types.Pkg, write bool) {
|
func (w *exportWriter) setPkg(pkg *types.Pkg, write bool) {
|
||||||
if pkg == nil {
|
if pkg == types.NoPkg {
|
||||||
// TODO(mdempsky): Proactively set Pkg for types and
|
base.Fatalf("missing pkg")
|
||||||
// remove this fallback logic.
|
|
||||||
pkg = ir.LocalPkg
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if write {
|
if write {
|
||||||
|
|
|
||||||
|
|
@ -545,9 +545,7 @@ func (r *importReader) typ1() *types.Type {
|
||||||
fs[i] = f
|
fs[i] = f
|
||||||
}
|
}
|
||||||
|
|
||||||
t := types.NewStruct(fs)
|
return types.NewStruct(r.currPkg, fs)
|
||||||
t.SetPkg(r.currPkg)
|
|
||||||
return t
|
|
||||||
|
|
||||||
case interfaceType:
|
case interfaceType:
|
||||||
r.setPkg()
|
r.setPkg()
|
||||||
|
|
@ -569,8 +567,7 @@ func (r *importReader) typ1() *types.Type {
|
||||||
methods[i] = types.NewField(pos, sym, typ)
|
methods[i] = types.NewField(pos, sym, typ)
|
||||||
}
|
}
|
||||||
|
|
||||||
t := types.NewInterface(append(embeddeds, methods...))
|
t := types.NewInterface(r.currPkg, append(embeddeds, methods...))
|
||||||
t.SetPkg(r.currPkg)
|
|
||||||
|
|
||||||
// Ensure we expand the interface in the frontend (#25055).
|
// Ensure we expand the interface in the frontend (#25055).
|
||||||
checkwidth(t)
|
checkwidth(t)
|
||||||
|
|
@ -588,9 +585,7 @@ func (r *importReader) signature(recv *types.Field) *types.Type {
|
||||||
if n := len(params); n > 0 {
|
if n := len(params); n > 0 {
|
||||||
params[n-1].SetIsDDD(r.bool())
|
params[n-1].SetIsDDD(r.bool())
|
||||||
}
|
}
|
||||||
t := types.NewSignature(recv, params, results)
|
return types.NewSignature(r.currPkg, recv, params, results)
|
||||||
t.SetPkg(r.currPkg)
|
|
||||||
return t
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *importReader) paramList() []*types.Field {
|
func (r *importReader) paramList() []*types.Field {
|
||||||
|
|
|
||||||
|
|
@ -14,13 +14,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func typeWithoutPointers() *types.Type {
|
func typeWithoutPointers() *types.Type {
|
||||||
return types.NewStruct([]*types.Field{
|
return types.NewStruct(types.NoPkg, []*types.Field{
|
||||||
types.NewField(src.NoXPos, nil, types.New(types.TINT)),
|
types.NewField(src.NoXPos, nil, types.New(types.TINT)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func typeWithPointers() *types.Type {
|
func typeWithPointers() *types.Type {
|
||||||
return types.NewStruct([]*types.Field{
|
return types.NewStruct(types.NoPkg, []*types.Field{
|
||||||
types.NewField(src.NoXPos, nil, types.NewPtr(types.New(types.TINT))),
|
types.NewField(src.NoXPos, nil, types.NewPtr(types.New(types.TINT))),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -126,7 +126,7 @@ func bmap(t *types.Type) *types.Type {
|
||||||
field = append(field, overflow)
|
field = append(field, overflow)
|
||||||
|
|
||||||
// link up fields
|
// link up fields
|
||||||
bucket := types.NewStruct(field[:])
|
bucket := types.NewStruct(types.NoPkg, field[:])
|
||||||
bucket.SetNoalg(true)
|
bucket.SetNoalg(true)
|
||||||
dowidth(bucket)
|
dowidth(bucket)
|
||||||
|
|
||||||
|
|
@ -220,7 +220,7 @@ func hmap(t *types.Type) *types.Type {
|
||||||
makefield("extra", types.Types[types.TUNSAFEPTR]),
|
makefield("extra", types.Types[types.TUNSAFEPTR]),
|
||||||
}
|
}
|
||||||
|
|
||||||
hmap := types.NewStruct(fields)
|
hmap := types.NewStruct(types.NoPkg, fields)
|
||||||
hmap.SetNoalg(true)
|
hmap.SetNoalg(true)
|
||||||
dowidth(hmap)
|
dowidth(hmap)
|
||||||
|
|
||||||
|
|
@ -283,7 +283,7 @@ func hiter(t *types.Type) *types.Type {
|
||||||
}
|
}
|
||||||
|
|
||||||
// build iterator struct holding the above fields
|
// build iterator struct holding the above fields
|
||||||
hiter := types.NewStruct(fields)
|
hiter := types.NewStruct(types.NoPkg, fields)
|
||||||
hiter.SetNoalg(true)
|
hiter.SetNoalg(true)
|
||||||
dowidth(hiter)
|
dowidth(hiter)
|
||||||
if hiter.Width != int64(12*Widthptr) {
|
if hiter.Width != int64(12*Widthptr) {
|
||||||
|
|
@ -329,7 +329,7 @@ func deferstruct(stksize int64) *types.Type {
|
||||||
}
|
}
|
||||||
|
|
||||||
// build struct holding the above fields
|
// build struct holding the above fields
|
||||||
s := types.NewStruct(fields)
|
s := types.NewStruct(types.NoPkg, fields)
|
||||||
s.SetNoalg(true)
|
s.SetNoalg(true)
|
||||||
s.Width = widstruct(s, s, 0, 1)
|
s.Width = widstruct(s, s, 0, 1)
|
||||||
s.Align = uint8(Widthptr)
|
s.Align = uint8(Widthptr)
|
||||||
|
|
|
||||||
|
|
@ -104,7 +104,7 @@ func initUniverse() {
|
||||||
}
|
}
|
||||||
|
|
||||||
types.Types[types.TANY] = types.New(types.TANY)
|
types.Types[types.TANY] = types.New(types.TANY)
|
||||||
types.Types[types.TINTER] = types.NewInterface(nil)
|
types.Types[types.TINTER] = types.NewInterface(ir.LocalPkg, nil)
|
||||||
|
|
||||||
defBasic := func(kind types.Kind, pkg *types.Pkg, name string) *types.Type {
|
defBasic := func(kind types.Kind, pkg *types.Pkg, name string) *types.Type {
|
||||||
sym := pkg.Lookup(name)
|
sym := pkg.Lookup(name)
|
||||||
|
|
@ -325,11 +325,11 @@ func initUniverse() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeErrorInterface() *types.Type {
|
func makeErrorInterface() *types.Type {
|
||||||
sig := types.NewSignature(fakeRecvField(), nil, []*types.Field{
|
sig := types.NewSignature(types.NoPkg, fakeRecvField(), nil, []*types.Field{
|
||||||
types.NewField(src.NoXPos, nil, types.Types[types.TSTRING]),
|
types.NewField(src.NoXPos, nil, types.Types[types.TSTRING]),
|
||||||
})
|
})
|
||||||
method := types.NewField(src.NoXPos, lookup("Error"), sig)
|
method := types.NewField(src.NoXPos, lookup("Error"), sig)
|
||||||
return types.NewInterface([]*types.Field{method})
|
return types.NewInterface(types.NoPkg, []*types.Field{method})
|
||||||
}
|
}
|
||||||
|
|
||||||
// finishUniverse makes the universe block visible within the current package.
|
// finishUniverse makes the universe block visible within the current package.
|
||||||
|
|
|
||||||
|
|
@ -211,6 +211,11 @@ func (t *Type) Pos() src.XPos {
|
||||||
return src.NoXPos
|
return src.NoXPos
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NoPkg is a nil *Pkg value for clarity.
|
||||||
|
// It's intended for use when constructing types that aren't exported
|
||||||
|
// and thus don't need to be associated with any package.
|
||||||
|
var NoPkg *Pkg = nil
|
||||||
|
|
||||||
// Pkg returns the package that t appeared in.
|
// Pkg returns the package that t appeared in.
|
||||||
//
|
//
|
||||||
// Pkg is only defined for function, struct, and interface types
|
// Pkg is only defined for function, struct, and interface types
|
||||||
|
|
@ -231,20 +236,6 @@ func (t *Type) Pkg() *Pkg {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetPkg sets the package that t appeared in.
|
|
||||||
func (t *Type) SetPkg(pkg *Pkg) {
|
|
||||||
switch t.kind {
|
|
||||||
case TFUNC:
|
|
||||||
t.Extra.(*Func).pkg = pkg
|
|
||||||
case TSTRUCT:
|
|
||||||
t.Extra.(*Struct).pkg = pkg
|
|
||||||
case TINTER:
|
|
||||||
t.Extra.(*Interface).pkg = pkg
|
|
||||||
default:
|
|
||||||
Fatalf("Pkg: unexpected kind: %v", t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Map contains Type fields specific to maps.
|
// Map contains Type fields specific to maps.
|
||||||
type Map struct {
|
type Map struct {
|
||||||
Key *Type // Key type
|
Key *Type // Key type
|
||||||
|
|
@ -1609,7 +1600,7 @@ func (t *Type) SetUnderlying(underlying *Type) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNamed returns a new basic type of the given kind.
|
// NewBasic returns a new basic type of the given kind.
|
||||||
func NewBasic(kind Kind, obj Object) *Type {
|
func NewBasic(kind Kind, obj Object) *Type {
|
||||||
t := New(kind)
|
t := New(kind)
|
||||||
t.sym = obj.Sym()
|
t.sym = obj.Sym()
|
||||||
|
|
@ -1619,18 +1610,19 @@ func NewBasic(kind Kind, obj Object) *Type {
|
||||||
|
|
||||||
// NewInterface returns a new interface for the given methods and
|
// NewInterface returns a new interface for the given methods and
|
||||||
// embedded types. Embedded types are specified as fields with no Sym.
|
// embedded types. Embedded types are specified as fields with no Sym.
|
||||||
func NewInterface(methods []*Field) *Type {
|
func NewInterface(pkg *Pkg, methods []*Field) *Type {
|
||||||
t := New(TINTER)
|
t := New(TINTER)
|
||||||
t.SetInterface(methods)
|
t.SetInterface(methods)
|
||||||
if anyBroke(methods) {
|
if anyBroke(methods) {
|
||||||
t.SetBroke(true)
|
t.SetBroke(true)
|
||||||
}
|
}
|
||||||
|
t.Extra.(*Interface).pkg = pkg
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSignature returns a new function type for the given receiver,
|
// NewSignature returns a new function type for the given receiver,
|
||||||
// parameters, and results, any of which may be nil.
|
// parameters, and results, any of which may be nil.
|
||||||
func NewSignature(recv *Field, params, results []*Field) *Type {
|
func NewSignature(pkg *Pkg, recv *Field, params, results []*Field) *Type {
|
||||||
var recvs []*Field
|
var recvs []*Field
|
||||||
if recv != nil {
|
if recv != nil {
|
||||||
recvs = []*Field{recv}
|
recvs = []*Field{recv}
|
||||||
|
|
@ -1640,7 +1632,7 @@ func NewSignature(recv *Field, params, results []*Field) *Type {
|
||||||
ft := t.FuncType()
|
ft := t.FuncType()
|
||||||
|
|
||||||
funargs := func(fields []*Field, funarg Funarg) *Type {
|
funargs := func(fields []*Field, funarg Funarg) *Type {
|
||||||
s := NewStruct(fields)
|
s := NewStruct(NoPkg, fields)
|
||||||
s.StructType().Funarg = funarg
|
s.StructType().Funarg = funarg
|
||||||
if s.Broke() {
|
if s.Broke() {
|
||||||
t.SetBroke(true)
|
t.SetBroke(true)
|
||||||
|
|
@ -1651,17 +1643,19 @@ func NewSignature(recv *Field, params, results []*Field) *Type {
|
||||||
ft.Receiver = funargs(recvs, FunargRcvr)
|
ft.Receiver = funargs(recvs, FunargRcvr)
|
||||||
ft.Params = funargs(params, FunargParams)
|
ft.Params = funargs(params, FunargParams)
|
||||||
ft.Results = funargs(results, FunargResults)
|
ft.Results = funargs(results, FunargResults)
|
||||||
|
ft.pkg = pkg
|
||||||
|
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStruct returns a new struct with the given fields.
|
// NewStruct returns a new struct with the given fields.
|
||||||
func NewStruct(fields []*Field) *Type {
|
func NewStruct(pkg *Pkg, fields []*Field) *Type {
|
||||||
t := New(TSTRUCT)
|
t := New(TSTRUCT)
|
||||||
t.SetFields(fields)
|
t.SetFields(fields)
|
||||||
if anyBroke(fields) {
|
if anyBroke(fields) {
|
||||||
t.SetBroke(true)
|
t.SetBroke(true)
|
||||||
}
|
}
|
||||||
|
t.Extra.(*Struct).pkg = pkg
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue