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