1
2
3
4
5 package types2
6
7 import (
8 "cmd/compile/internal/syntax"
9 . "internal/types/errors"
10 )
11
12
13
14
15
16 type Interface struct {
17 check *Checker
18 methods []*Func
19 embeddeds []Type
20 embedPos *[]syntax.Pos
21 implicit bool
22 complete bool
23
24 tset *_TypeSet
25 }
26
27
28 func (t *Interface) typeSet() *_TypeSet { return computeInterfaceTypeSet(t.check, nopos, t) }
29
30
31 var emptyInterface = Interface{complete: true, tset: &topTypeSet}
32
33
34
35
36 func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface {
37 if len(methods) == 0 && len(embeddeds) == 0 {
38 return &emptyInterface
39 }
40
41
42 typ := (*Checker)(nil).newInterface()
43 for _, m := range methods {
44 if sig := m.typ.(*Signature); sig.recv == nil {
45 sig.recv = newVar(RecvVar, m.pos, m.pkg, "", typ)
46 }
47 }
48
49
50 sortMethods(methods)
51
52 typ.methods = methods
53 typ.embeddeds = embeddeds
54 typ.complete = true
55
56 return typ
57 }
58
59
60 func (check *Checker) newInterface() *Interface {
61 typ := &Interface{check: check}
62 if check != nil {
63 check.needsCleanup(typ)
64 }
65 return typ
66 }
67
68
69
70
71
72 func (t *Interface) MarkImplicit() {
73 t.implicit = true
74 }
75
76
77 func (t *Interface) NumExplicitMethods() int { return len(t.methods) }
78
79
80
81 func (t *Interface) ExplicitMethod(i int) *Func { return t.methods[i] }
82
83
84 func (t *Interface) NumEmbeddeds() int { return len(t.embeddeds) }
85
86
87 func (t *Interface) EmbeddedType(i int) Type { return t.embeddeds[i] }
88
89
90 func (t *Interface) NumMethods() int { return t.typeSet().NumMethods() }
91
92
93
94 func (t *Interface) Method(i int) *Func { return t.typeSet().Method(i) }
95
96
97 func (t *Interface) Empty() bool { return t.typeSet().IsAll() }
98
99
100 func (t *Interface) IsComparable() bool { return t.typeSet().IsComparable(nil) }
101
102
103 func (t *Interface) IsMethodSet() bool { return t.typeSet().IsMethodSet() }
104
105
106 func (t *Interface) IsImplicit() bool { return t.implicit }
107
108 func (t *Interface) Underlying() Type { return t }
109 func (t *Interface) String() string { return TypeString(t, nil) }
110
111
112
113
114 func (t *Interface) cleanup() {
115 t.typeSet()
116 t.check = nil
117 t.embedPos = nil
118 }
119
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)
125 }
126 *ityp.embedPos = append(*ityp.embedPos, pos)
127 }
128
129 for _, f := range iface.MethodList {
130 if f.Name == nil {
131 addEmbedded(atPos(f.Type), parseUnion(check, f.Type))
132 continue
133 }
134
135
136
137 name := f.Name.Value
138 if name == "_" {
139 check.error(f.Name, BlankIfaceMethod, "methods must have a unique non-blank name")
140 continue
141 }
142
143
144
145
146 ftyp, _ := f.Type.(*syntax.FuncType)
147 if ftyp == nil {
148 check.errorf(f.Type, InvalidSyntaxTree, "%s is not a method signature", f.Type)
149 continue
150 }
151 sig := new(Signature)
152 check.funcType(sig, nil, nil, ftyp)
153
154
155 var recvTyp Type = ityp
156 if def != nil {
157 if named := asNamed(def.typ); named != nil {
158 recvTyp = named
159 }
160 }
161 sig.recv = newVar(RecvVar, f.Name.Pos(), check.pkg, "", recvTyp)
162
163 m := NewFunc(f.Name.Pos(), check.pkg, name, sig)
164 check.recordDef(f.Name, m)
165 ityp.methods = append(ityp.methods, m)
166 }
167
168
169
170 ityp.complete = true
171
172 if len(ityp.methods) == 0 && len(ityp.embeddeds) == 0 {
173
174 ityp.tset = &topTypeSet
175 return
176 }
177
178
179
180 sortMethods(ityp.methods)
181
182
183
184
185 check.later(func() {
186 computeInterfaceTypeSet(check, iface.Pos(), ityp)
187 }).describef(iface, "compute type set for %s", ityp)
188 }
189
View as plain text