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