1
2
3
4
5 package types2
6
7 import (
8 "cmd/compile/internal/syntax"
9 "fmt"
10 . "internal/types/errors"
11 "path/filepath"
12 "strings"
13 )
14
15
16
17
18
19
20 type Signature struct {
21
22
23
24
25 rparams *TypeParamList
26 tparams *TypeParamList
27 scope *Scope
28 recv *Var
29 params *Tuple
30 results *Tuple
31 variadic bool
32 }
33
34
35
36
37
38
39
40 func NewSignatureType(recv *Var, recvTypeParams, typeParams []*TypeParam, params, results *Tuple, variadic bool) *Signature {
41 if variadic {
42 n := params.Len()
43 if n == 0 {
44 panic("variadic function must have at least one parameter")
45 }
46 core := coreString(params.At(n - 1).typ)
47 if _, ok := core.(*Slice); !ok && !isString(core) {
48 panic(fmt.Sprintf("got %s, want variadic parameter with unnamed slice type or string as core type", core.String()))
49 }
50 }
51 sig := &Signature{recv: recv, params: params, results: results, variadic: variadic}
52 if len(recvTypeParams) != 0 {
53 if recv == nil {
54 panic("function with receiver type parameters must have a receiver")
55 }
56 sig.rparams = bindTParams(recvTypeParams)
57 }
58 if len(typeParams) != 0 {
59 if recv != nil {
60 panic("function with type parameters cannot have a receiver")
61 }
62 sig.tparams = bindTParams(typeParams)
63 }
64 return sig
65 }
66
67
68
69
70
71
72
73 func (s *Signature) Recv() *Var { return s.recv }
74
75
76 func (s *Signature) TypeParams() *TypeParamList { return s.tparams }
77
78
79 func (s *Signature) RecvTypeParams() *TypeParamList { return s.rparams }
80
81
82 func (s *Signature) Params() *Tuple { return s.params }
83
84
85 func (s *Signature) Results() *Tuple { return s.results }
86
87
88 func (s *Signature) Variadic() bool { return s.variadic }
89
90 func (s *Signature) Underlying() Type { return s }
91 func (s *Signature) String() string { return TypeString(s, nil) }
92
93
94
95
96
97 func (check *Checker) funcType(sig *Signature, recvPar *syntax.Field, tparams []*syntax.Field, ftyp *syntax.FuncType) {
98 check.openScope(ftyp, "function")
99 check.scope.isFunc = true
100 check.recordScope(ftyp, check.scope)
101 sig.scope = check.scope
102 defer check.closeScope()
103
104
105 var recv *Var
106 var rparams *TypeParamList
107 if recvPar != nil {
108
109 scopePos := ftyp.Pos()
110 recv, rparams = check.collectRecv(recvPar, scopePos)
111 }
112
113
114 if tparams != nil {
115
116 check.collectTypeParams(&sig.tparams, tparams)
117 }
118
119
120 pnames, params, variadic := check.collectParams(ftyp.ParamList, true)
121 rnames, results, _ := check.collectParams(ftyp.ResultList, false)
122
123
124 scopePos := syntax.EndPos(ftyp)
125 if recv != nil && recv.name != "" {
126 check.declare(check.scope, recvPar.Name, recv, scopePos)
127 }
128 check.declareParams(pnames, params, scopePos)
129 check.declareParams(rnames, results, scopePos)
130
131 sig.recv = recv
132 sig.rparams = rparams
133 sig.params = NewTuple(params...)
134 sig.results = NewTuple(results...)
135 sig.variadic = variadic
136 }
137
138
139
140
141 func (check *Checker) collectRecv(rparam *syntax.Field, scopePos syntax.Pos) (*Var, *TypeParamList) {
142
143
144
145
146
147
148 rptr, rbase, rtparams := check.unpackRecv(rparam.Type, true)
149
150
151 var recvType Type = Typ[Invalid]
152 var recvTParamsList *TypeParamList
153 if rtparams == nil {
154
155
156
157
158
159 recvType = check.varType(rparam.Type)
160
161
162
163
164 a, _ := unpointer(recvType).(*Alias)
165 for a != nil {
166 baseType := unpointer(a.fromRHS)
167 if g, _ := baseType.(genericType); g != nil && g.TypeParams() != nil {
168 check.errorf(rbase, InvalidRecv, "cannot define new methods on instantiated type %s", g)
169 recvType = Typ[Invalid]
170 break
171 }
172 a, _ = baseType.(*Alias)
173 }
174 } else {
175
176
177
178 var baseType *Named
179 var cause string
180 if t := check.genericType(rbase, &cause); isValid(t) {
181 switch t := t.(type) {
182 case *Named:
183 baseType = t
184 case *Alias:
185
186
187 if isValid(unalias(t)) {
188 check.errorf(rbase, InvalidRecv, "cannot define new methods on generic alias type %s", t)
189 }
190
191
192 default:
193 panic("unreachable")
194 }
195 } else {
196 if cause != "" {
197 check.errorf(rbase, InvalidRecv, "%s", cause)
198 }
199
200 }
201
202
203
204
205
206 recvTParams := make([]*TypeParam, len(rtparams))
207 for i, rparam := range rtparams {
208 tpar := check.declareTypeParam(rparam, scopePos)
209 recvTParams[i] = tpar
210
211
212
213 check.recordUse(rparam, tpar.obj)
214 check.recordTypeAndValue(rparam, typexpr, tpar, nil)
215 }
216 recvTParamsList = bindTParams(recvTParams)
217
218
219
220 if baseType != nil {
221 baseTParams := baseType.TypeParams().list()
222 if len(recvTParams) == len(baseTParams) {
223 smap := makeRenameMap(baseTParams, recvTParams)
224 for i, recvTPar := range recvTParams {
225 baseTPar := baseTParams[i]
226 check.mono.recordCanon(recvTPar, baseTPar)
227
228
229
230 recvTPar.bound = check.subst(recvTPar.obj.pos, baseTPar.bound, smap, nil, check.context())
231 }
232 } else {
233 got := measure(len(recvTParams), "type parameter")
234 check.errorf(rbase, BadRecv, "receiver declares %s, but receiver base type declares %d", got, len(baseTParams))
235 }
236
237
238
239 check.verifyVersionf(rbase, go1_18, "type instantiation")
240 targs := make([]Type, len(recvTParams))
241 for i, targ := range recvTParams {
242 targs[i] = targ
243 }
244 recvType = check.instance(rparam.Type.Pos(), baseType, targs, nil, check.context())
245 check.recordInstance(rbase, targs, recvType)
246
247
248 if rptr && isValid(recvType) {
249 recvType = NewPointer(recvType)
250 }
251
252 check.recordParenthesizedRecvTypes(rparam.Type, recvType)
253 }
254 }
255
256
257
258 var recv *Var
259 if rname := rparam.Name; rname != nil && rname.Value != "" {
260
261 recv = NewParam(rname.Pos(), check.pkg, rname.Value, recvType)
262
263
264
265 } else {
266
267 recv = NewParam(rparam.Pos(), check.pkg, "", recvType)
268 check.recordImplicit(rparam, recv)
269 }
270
271
272
273 check.later(func() {
274 check.validRecv(rbase, recv)
275 }).describef(recv, "validRecv(%s)", recv)
276
277 return recv, recvTParamsList
278 }
279
280 func unpointer(t Type) Type {
281 for {
282 p, _ := t.(*Pointer)
283 if p == nil {
284 return t
285 }
286 t = p.base
287 }
288 }
289
290
291
292
293
294
295
296
297
298
299
300 func (check *Checker) recordParenthesizedRecvTypes(expr syntax.Expr, typ Type) {
301 for {
302 check.recordTypeAndValue(expr, typexpr, typ, nil)
303 switch e := expr.(type) {
304 case *syntax.ParenExpr:
305 expr = e.X
306 case *syntax.Operation:
307 if e.Op == syntax.Mul && e.Y == nil {
308 expr = e.X
309
310
311 ptr, _ := typ.(*Pointer)
312 if ptr == nil {
313 return
314 }
315 typ = ptr.base
316 break
317 }
318 return
319 default:
320 return
321 }
322 }
323 }
324
325
326
327
328 func (check *Checker) collectParams(list []*syntax.Field, variadicOk bool) (names []*syntax.Name, params []*Var, variadic bool) {
329 if list == nil {
330 return
331 }
332
333 var named, anonymous bool
334
335 var typ Type
336 var prev syntax.Expr
337 for i, field := range list {
338 ftype := field.Type
339
340 if ftype != prev {
341 prev = ftype
342 if t, _ := ftype.(*syntax.DotsType); t != nil {
343 ftype = t.Elem
344 if variadicOk && i == len(list)-1 {
345 variadic = true
346 } else {
347 check.softErrorf(t, MisplacedDotDotDot, "can only use ... with final parameter in list")
348
349 }
350 }
351 typ = check.varType(ftype)
352 }
353
354
355 if field.Name != nil {
356
357 name := field.Name.Value
358 if name == "" {
359 check.error(field.Name, InvalidSyntaxTree, "anonymous parameter")
360
361 }
362 par := NewParam(field.Name.Pos(), check.pkg, name, typ)
363
364 names = append(names, field.Name)
365 params = append(params, par)
366 named = true
367 } else {
368
369 par := NewParam(field.Pos(), check.pkg, "", typ)
370 check.recordImplicit(field, par)
371 names = append(names, nil)
372 params = append(params, par)
373 anonymous = true
374 }
375 }
376
377 if named && anonymous {
378 check.error(list[0], InvalidSyntaxTree, "list contains both named and anonymous parameters")
379
380 }
381
382
383
384
385 if variadic {
386 last := params[len(params)-1]
387 last.typ = &Slice{elem: last.typ}
388 check.recordTypeAndValue(list[len(list)-1].Type, typexpr, last.typ, nil)
389 }
390
391 return
392 }
393
394
395 func (check *Checker) declareParams(names []*syntax.Name, params []*Var, scopePos syntax.Pos) {
396 for i, name := range names {
397 if name != nil && name.Value != "" {
398 check.declare(check.scope, name, params[i], scopePos)
399 }
400 }
401 }
402
403
404
405 func (check *Checker) validRecv(pos poser, recv *Var) {
406
407 rtyp, _ := deref(recv.typ)
408 atyp := Unalias(rtyp)
409 if !isValid(atyp) {
410 return
411 }
412
413
414
415 switch T := atyp.(type) {
416 case *Named:
417 if T.obj.pkg != check.pkg || isCGoTypeObj(T.obj) {
418 check.errorf(pos, InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
419 break
420 }
421 var cause string
422 switch u := T.under().(type) {
423 case *Basic:
424
425 if u.kind == UnsafePointer {
426 cause = "unsafe.Pointer"
427 }
428 case *Pointer, *Interface:
429 cause = "pointer or interface type"
430 case *TypeParam:
431
432
433 panic("unreachable")
434 }
435 if cause != "" {
436 check.errorf(pos, InvalidRecv, "invalid receiver type %s (%s)", rtyp, cause)
437 }
438 case *Basic:
439 check.errorf(pos, InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
440 default:
441 check.errorf(pos, InvalidRecv, "invalid receiver type %s", recv.typ)
442 }
443 }
444
445
446 func isCGoTypeObj(obj *TypeName) bool {
447 return strings.HasPrefix(obj.name, "_Ctype_") ||
448 strings.HasPrefix(filepath.Base(obj.pos.FileBase().Filename()), "_cgo_")
449 }
450
View as plain text