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