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