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
162 check.collectTypeParams(&sig.tparams, tparams)
163 }
164
165
166 pnames, params, variadic := check.collectParams(ParamVar, ftyp.ParamList)
167 rnames, results, _ := check.collectParams(ResultVar, ftyp.ResultList)
168
169
170 scopePos := syntax.EndPos(ftyp)
171 if recv != nil && recv.name != "" {
172 check.declare(check.scope, recvPar.Name, recv, scopePos)
173 }
174 check.declareParams(pnames, params, scopePos)
175 check.declareParams(rnames, results, scopePos)
176
177 sig.recv = recv
178 sig.rparams = rparams
179 sig.params = NewTuple(params...)
180 sig.results = NewTuple(results...)
181 sig.variadic = variadic
182 }
183
184
185
186
187 func (check *Checker) collectRecv(rparam *syntax.Field, scopePos syntax.Pos) (*Var, *TypeParamList) {
188
189
190
191
192
193
194 rptr, rbase, rtparams := check.unpackRecv(rparam.Type, true)
195
196
197 var recvType Type = Typ[Invalid]
198 var recvTParamsList *TypeParamList
199 if rtparams == nil {
200
201
202
203
204
205 recvType = check.varType(rparam.Type)
206
207
208
209
210 a, _ := unpointer(recvType).(*Alias)
211 for a != nil {
212 baseType := unpointer(a.fromRHS)
213 if g, _ := baseType.(genericType); g != nil && g.TypeParams() != nil {
214 check.errorf(rbase, InvalidRecv, "cannot define new methods on instantiated type %s", g)
215 recvType = Typ[Invalid]
216 break
217 }
218 a, _ = baseType.(*Alias)
219 }
220 } else {
221
222
223
224 var baseType *Named
225 var cause string
226 if t := check.genericType(rbase, &cause); isValid(t) {
227 switch t := t.(type) {
228 case *Named:
229 baseType = t
230 case *Alias:
231
232
233 if isValid(t) {
234 check.errorf(rbase, InvalidRecv, "cannot define new methods on generic alias type %s", t)
235 }
236
237
238 default:
239 panic("unreachable")
240 }
241 } else {
242 if cause != "" {
243 check.errorf(rbase, InvalidRecv, "%s", cause)
244 }
245
246 }
247
248
249
250
251
252 recvTParams := make([]*TypeParam, len(rtparams))
253 for i, rparam := range rtparams {
254 tpar := check.declareTypeParam(rparam, scopePos)
255 recvTParams[i] = tpar
256
257
258
259 check.recordUse(rparam, tpar.obj)
260 check.recordTypeAndValue(rparam, typexpr, tpar, nil)
261 }
262 recvTParamsList = bindTParams(recvTParams)
263
264
265
266 if baseType != nil {
267 baseTParams := baseType.TypeParams().list()
268 if len(recvTParams) == len(baseTParams) {
269 smap := makeRenameMap(baseTParams, recvTParams)
270 for i, recvTPar := range recvTParams {
271 baseTPar := baseTParams[i]
272 check.mono.recordCanon(recvTPar, baseTPar)
273
274
275
276 recvTPar.bound = check.subst(recvTPar.obj.pos, baseTPar.bound, smap, nil, check.context())
277 }
278 } else {
279 got := measure(len(recvTParams), "type parameter")
280 check.errorf(rbase, BadRecv, "receiver declares %s, but receiver base type declares %d", got, len(baseTParams))
281 }
282
283
284
285 check.verifyVersionf(rbase, go1_18, "type instantiation")
286 targs := make([]Type, len(recvTParams))
287 for i, targ := range recvTParams {
288 targs[i] = targ
289 }
290 recvType = check.instance(rparam.Type.Pos(), baseType, targs, nil, check.context())
291 check.recordInstance(rbase, targs, recvType)
292
293
294 if rptr && isValid(recvType) {
295 recvType = NewPointer(recvType)
296 }
297
298 check.recordParenthesizedRecvTypes(rparam.Type, recvType)
299 }
300 }
301
302
303
304 var recv *Var
305 if rname := rparam.Name; rname != nil && rname.Value != "" {
306
307 recv = newVar(RecvVar, rname.Pos(), check.pkg, rname.Value, recvType)
308
309
310
311 } else {
312
313 recv = newVar(RecvVar, rparam.Pos(), check.pkg, "", recvType)
314 check.recordImplicit(rparam, recv)
315 }
316
317
318
319 check.later(func() {
320 check.validRecv(rbase, recv)
321 }).describef(recv, "validRecv(%s)", recv)
322
323 return recv, recvTParamsList
324 }
325
326 func unpointer(t Type) Type {
327 for {
328 p, _ := t.(*Pointer)
329 if p == nil {
330 return t
331 }
332 t = p.base
333 }
334 }
335
336
337
338
339
340
341
342
343
344
345
346 func (check *Checker) recordParenthesizedRecvTypes(expr syntax.Expr, typ Type) {
347 for {
348 check.recordTypeAndValue(expr, typexpr, typ, nil)
349 switch e := expr.(type) {
350 case *syntax.ParenExpr:
351 expr = e.X
352 case *syntax.Operation:
353 if e.Op == syntax.Mul && e.Y == nil {
354 expr = e.X
355
356
357 ptr, _ := typ.(*Pointer)
358 if ptr == nil {
359 return
360 }
361 typ = ptr.base
362 break
363 }
364 return
365 default:
366 return
367 }
368 }
369 }
370
371
372
373
374
375 func (check *Checker) collectParams(kind VarKind, list []*syntax.Field) (names []*syntax.Name, params []*Var, variadic bool) {
376 if list == nil {
377 return
378 }
379
380 var named, anonymous bool
381
382 var typ Type
383 var prev syntax.Expr
384 for i, field := range list {
385 ftype := field.Type
386
387 if ftype != prev {
388 prev = ftype
389 if t, _ := ftype.(*syntax.DotsType); t != nil {
390 ftype = t.Elem
391 if kind == ParamVar && i == len(list)-1 {
392 variadic = true
393 } else {
394 check.error(t, InvalidSyntaxTree, "invalid use of ...")
395
396 }
397 }
398 typ = check.varType(ftype)
399 }
400
401
402 if field.Name != nil {
403
404 name := field.Name.Value
405 if name == "" {
406 check.error(field.Name, InvalidSyntaxTree, "anonymous parameter")
407
408 }
409 par := newVar(kind, field.Name.Pos(), check.pkg, name, typ)
410
411 names = append(names, field.Name)
412 params = append(params, par)
413 named = true
414 } else {
415
416 par := newVar(kind, field.Pos(), check.pkg, "", typ)
417 check.recordImplicit(field, par)
418 names = append(names, nil)
419 params = append(params, par)
420 anonymous = true
421 }
422 }
423
424 if named && anonymous {
425 check.error(list[0], InvalidSyntaxTree, "list contains both named and anonymous parameters")
426
427 }
428
429
430
431
432 if variadic {
433 last := params[len(params)-1]
434 last.typ = &Slice{elem: last.typ}
435 check.recordTypeAndValue(list[len(list)-1].Type, typexpr, last.typ, nil)
436 }
437
438 return
439 }
440
441
442 func (check *Checker) declareParams(names []*syntax.Name, params []*Var, scopePos syntax.Pos) {
443 for i, name := range names {
444 if name != nil && name.Value != "" {
445 check.declare(check.scope, name, params[i], scopePos)
446 }
447 }
448 }
449
450
451
452 func (check *Checker) validRecv(pos poser, recv *Var) {
453
454 rtyp, _ := deref(recv.typ)
455 atyp := Unalias(rtyp)
456 if !isValid(atyp) {
457 return
458 }
459
460
461
462 switch T := atyp.(type) {
463 case *Named:
464 if T.obj.pkg != check.pkg || isCGoTypeObj(T.obj) {
465 check.errorf(pos, InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
466 break
467 }
468 var cause string
469 switch u := T.Underlying().(type) {
470 case *Basic:
471
472 if u.kind == UnsafePointer {
473 cause = "unsafe.Pointer"
474 }
475 case *Pointer, *Interface:
476 cause = "pointer or interface type"
477 case *TypeParam:
478
479
480 panic("unreachable")
481 }
482 if cause != "" {
483 check.errorf(pos, InvalidRecv, "invalid receiver type %s (%s)", rtyp, cause)
484 }
485 case *Basic:
486 check.errorf(pos, InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
487 default:
488 check.errorf(pos, InvalidRecv, "invalid receiver type %s", recv.typ)
489 }
490 }
491
492
493 func isCGoTypeObj(obj *TypeName) bool {
494 return strings.HasPrefix(obj.name, "_Ctype_") ||
495 strings.HasPrefix(filepath.Base(obj.pos.FileBase().Filename()), "_cgo_")
496 }
497
View as plain text