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