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