1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
8 "cmd/compile/internal/syntax"
9 . "internal/types/errors"
12 // ----------------------------------------------------------------------------
15 // An Interface represents an interface type.
16 type Interface struct {
17 check *Checker // for error reporting; nil once type set is computed
18 methods []*Func // ordered list of explicitly declared methods
19 embeddeds []Type // ordered list of explicitly embedded elements
20 embedPos *[]syntax.Pos // positions of embedded elements; or nil (for error messages) - use pointer to save space
21 implicit bool // interface is wrapper for type set literal (non-interface T, ~T, or A|B)
22 complete bool // indicates that all fields (except for tset) are set up
24 tset *_TypeSet // type set described by this interface, computed lazily
27 // typeSet returns the type set for interface t.
28 func (t *Interface) typeSet() *_TypeSet { return computeInterfaceTypeSet(t.check, nopos, t) }
30 // emptyInterface represents the empty interface
31 var emptyInterface = Interface{complete: true, tset: &topTypeSet}
33 // NewInterfaceType returns a new interface for the given methods and embedded types.
34 // NewInterfaceType takes ownership of the provided methods and may modify their types
35 // by setting missing receivers.
36 func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface {
37 if len(methods) == 0 && len(embeddeds) == 0 {
38 return &emptyInterface
41 // set method receivers if necessary
42 typ := (*Checker)(nil).newInterface()
43 for _, m := range methods {
44 if sig := m.typ.(*Signature); sig.recv == nil {
45 sig.recv = NewVar(m.pos, m.pkg, "", typ)
49 // sort for API stability
53 typ.embeddeds = embeddeds
60 func (check *Checker) newInterface() *Interface {
61 typ := &Interface{check: check}
63 check.needsCleanup(typ)
68 // MarkImplicit marks the interface t as implicit, meaning this interface
69 // corresponds to a constraint literal such as ~T or A|B without explicit
70 // interface embedding. MarkImplicit should be called before any concurrent use
71 // of implicit interfaces.
72 func (t *Interface) MarkImplicit() {
76 // NumExplicitMethods returns the number of explicitly declared methods of interface t.
77 func (t *Interface) NumExplicitMethods() int { return len(t.methods) }
79 // ExplicitMethod returns the i'th explicitly declared method of interface t for 0 <= i < t.NumExplicitMethods().
80 // The methods are ordered by their unique Id.
81 func (t *Interface) ExplicitMethod(i int) *Func { return t.methods[i] }
83 // NumEmbeddeds returns the number of embedded types in interface t.
84 func (t *Interface) NumEmbeddeds() int { return len(t.embeddeds) }
86 // EmbeddedType returns the i'th embedded type of interface t for 0 <= i < t.NumEmbeddeds().
87 func (t *Interface) EmbeddedType(i int) Type { return t.embeddeds[i] }
89 // NumMethods returns the total number of methods of interface t.
90 func (t *Interface) NumMethods() int { return t.typeSet().NumMethods() }
92 // Method returns the i'th method of interface t for 0 <= i < t.NumMethods().
93 // The methods are ordered by their unique Id.
94 func (t *Interface) Method(i int) *Func { return t.typeSet().Method(i) }
96 // Empty reports whether t is the empty interface.
97 func (t *Interface) Empty() bool { return t.typeSet().IsAll() }
99 // IsComparable reports whether each type in interface t's type set is comparable.
100 func (t *Interface) IsComparable() bool { return t.typeSet().IsComparable(nil) }
102 // IsMethodSet reports whether the interface t is fully described by its method set.
103 func (t *Interface) IsMethodSet() bool { return t.typeSet().IsMethodSet() }
105 // IsImplicit reports whether the interface t is a wrapper for a type set literal.
106 func (t *Interface) IsImplicit() bool { return t.implicit }
108 func (t *Interface) Underlying() Type { return t }
109 func (t *Interface) String() string { return TypeString(t, nil) }
111 // ----------------------------------------------------------------------------
114 func (t *Interface) cleanup() {
115 t.typeSet() // any interface that escapes type checking must be safe for concurrent use
120 func (check *Checker) interfaceType(ityp *Interface, iface *syntax.InterfaceType, def *TypeName) {
121 addEmbedded := func(pos syntax.Pos, typ Type) {
122 ityp.embeddeds = append(ityp.embeddeds, typ)
123 if ityp.embedPos == nil {
124 ityp.embedPos = new([]syntax.Pos)
126 *ityp.embedPos = append(*ityp.embedPos, pos)
129 for _, f := range iface.MethodList {
131 addEmbedded(atPos(f.Type), parseUnion(check, f.Type))
136 // We have a method with name f.Name.
139 check.error(f.Name, BlankIfaceMethod, "methods must have a unique non-blank name")
143 typ := check.typ(f.Type)
144 sig, _ := typ.(*Signature)
147 check.errorf(f.Type, InvalidSyntaxTree, "%s is not a method signature", typ)
152 // use named receiver type if available (for better error messages)
153 var recvTyp Type = ityp
155 if named, _ := def.typ.(*Named); named != nil {
159 sig.recv = NewVar(f.Name.Pos(), check.pkg, "", recvTyp)
161 m := NewFunc(f.Name.Pos(), check.pkg, name, sig)
162 check.recordDef(f.Name, m)
163 ityp.methods = append(ityp.methods, m)
166 // All methods and embedded elements for this interface are collected;
167 // i.e., this interface may be used in a type set computation.
170 if len(ityp.methods) == 0 && len(ityp.embeddeds) == 0 {
172 ityp.tset = &topTypeSet
176 // sort for API stability
177 // (don't sort embeddeds: they must correspond to *embedPos entries)
178 sortMethods(ityp.methods)
180 // Compute type set as soon as possible to report any errors.
181 // Subsequent uses of type sets will use this computed type
182 // set and won't need to pass in a *Checker.
184 computeInterfaceTypeSet(check, iface.Pos(), ityp)
185 }).describef(iface, "compute type set for %s", ityp)