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