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