diff options
author | Matthew Dempsky <mdempsky@google.com> | 2020-12-01 13:07:35 -0800 |
---|---|---|
committer | Matthew Dempsky <mdempsky@google.com> | 2020-12-02 06:42:17 +0000 |
commit | 77a71e0057357b0567cc5036f7e0f903d82705bb (patch) | |
tree | e8d28d921a4aeab51276f896754e61787654d2bd /src/cmd/compile/internal/types/type.go | |
parent | 15085f89746762e0919fa257feac3eb5b996e6db (diff) | |
download | go-77a71e0057357b0567cc5036f7e0f903d82705bb.tar.gz go-77a71e0057357b0567cc5036f7e0f903d82705bb.zip |
[dev.regabi] cmd/compile: add Interface, Signature, and Struct constructors
This CL adds the remaining constructors needed to abstract away
construction of Types, and updates the compiler to use them
throughout. There's now just a couple uses within test cases to
remove.
While at it, I also replace the Func.Outnamed field with a simple
helper function, which reduces the size of function types somewhat.
Passes toolstash/buildall.
Change-Id: If1aa1095c98ae34b00380d0b3531bd63c10ce885
Reviewed-on: https://go-review.googlesource.com/c/go/+/274713
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/types/type.go')
-rw-r--r-- | src/cmd/compile/internal/types/type.go | 59 |
1 files changed, 57 insertions, 2 deletions
diff --git a/src/cmd/compile/internal/types/type.go b/src/cmd/compile/internal/types/type.go index 36aac53124..2eff8e3ba4 100644 --- a/src/cmd/compile/internal/types/type.go +++ b/src/cmd/compile/internal/types/type.go @@ -285,8 +285,6 @@ type Func struct { // It gets calculated via a temporary TFUNCARGS type. // Note that TFUNC's Width is Widthptr. Argwid int64 - - Outnamed bool } // FuncType returns t's extra func-specific fields. @@ -1618,3 +1616,60 @@ func NewBasic(kind Kind, obj Object) *Type { t.nod = obj return t } + +// NewInterface returns a new interface for the given methods and +// embedded types. Embedded types are specified as fields with no Sym. +func NewInterface(methods []*Field) *Type { + t := New(TINTER) + t.SetInterface(methods) + if anyBroke(methods) { + t.SetBroke(true) + } + return t +} + +// NewSignature returns a new function type for the given receiver, +// parameters, and results, any of which may be nil. +func NewSignature(recv *Field, params, results []*Field) *Type { + var recvs []*Field + if recv != nil { + recvs = []*Field{recv} + } + + t := New(TFUNC) + ft := t.FuncType() + + funargs := func(fields []*Field, funarg Funarg) *Type { + s := NewStruct(fields) + s.StructType().Funarg = funarg + if s.Broke() { + t.SetBroke(true) + } + return s + } + + ft.Receiver = funargs(recvs, FunargRcvr) + ft.Params = funargs(params, FunargParams) + ft.Results = funargs(results, FunargResults) + + return t +} + +// NewStruct returns a new struct with the given fields. +func NewStruct(fields []*Field) *Type { + t := New(TSTRUCT) + t.SetFields(fields) + if anyBroke(fields) { + t.SetBroke(true) + } + return t +} + +func anyBroke(fields []*Field) bool { + for _, f := range fields { + if f.Broke() { + return true + } + } + return false +} |