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