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
159 check.collectTypeParams(&sig.tparams, tparams)
160 }
161
162
163 pnames, params, variadic := check.collectParams(ParamVar, ftyp.ParamList)
164 rnames, results, _ := check.collectParams(ResultVar, ftyp.ResultList)
165
166
167 scopePos := syntax.EndPos(ftyp)
168 if recv != nil && recv.name != "" {
169 check.declare(check.scope, recvPar.Name, 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 *syntax.Field, scopePos syntax.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(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
301 var recv *Var
302 if rname := rparam.Name; rname != nil && rname.Value != "" {
303
304 recv = newVar(RecvVar, rname.Pos(), check.pkg, rname.Value, recvType)
305
306
307
308 } else {
309
310 recv = newVar(RecvVar, rparam.Pos(), check.pkg, "", recvType)
311 check.recordImplicit(rparam, recv)
312 }
313
314
315
316 check.later(func() {
317 check.validRecv(rbase, recv)
318 }).describef(recv, "validRecv(%s)", recv)
319
320 return recv, recvTParamsList
321 }
322
323 func unpointer(t Type) Type {
324 for {
325 p, _ := t.(*Pointer)
326 if p == nil {
327 return t
328 }
329 t = p.base
330 }
331 }
332
333
334
335
336
337
338
339
340
341
342
343 func (check *Checker) recordParenthesizedRecvTypes(expr syntax.Expr, typ Type) {
344 for {
345 check.recordTypeAndValue(expr, typexpr, typ, nil)
346 switch e := expr.(type) {
347 case *syntax.ParenExpr:
348 expr = e.X
349 case *syntax.Operation:
350 if e.Op == syntax.Mul && e.Y == nil {
351 expr = e.X
352
353
354 ptr, _ := typ.(*Pointer)
355 if ptr == nil {
356 return
357 }
358 typ = ptr.base
359 break
360 }
361 return
362 default:
363 return
364 }
365 }
366 }
367
368
369
370
371
372 func (check *Checker) collectParams(kind VarKind, list []*syntax.Field) (names []*syntax.Name, params []*Var, variadic bool) {
373 if list == nil {
374 return
375 }
376
377 var named, anonymous bool
378
379 var typ Type
380 var prev syntax.Expr
381 for i, field := range list {
382 ftype := field.Type
383
384 if ftype != prev {
385 prev = ftype
386 if t, _ := ftype.(*syntax.DotsType); t != nil {
387 ftype = t.Elem
388 if kind == ParamVar && i == len(list)-1 {
389 variadic = true
390 } else {
391 check.error(t, InvalidSyntaxTree, "invalid use of ...")
392
393 }
394 }
395 typ = check.varType(ftype)
396 }
397
398
399 if field.Name != nil {
400
401 name := field.Name.Value
402 if name == "" {
403 check.error(field.Name, InvalidSyntaxTree, "anonymous parameter")
404
405 }
406 par := newVar(kind, field.Name.Pos(), check.pkg, name, typ)
407
408 names = append(names, field.Name)
409 params = append(params, par)
410 named = true
411 } else {
412
413 par := newVar(kind, field.Pos(), check.pkg, "", typ)
414 check.recordImplicit(field, par)
415 names = append(names, nil)
416 params = append(params, par)
417 anonymous = true
418 }
419 }
420
421 if named && anonymous {
422 check.error(list[0], InvalidSyntaxTree, "list contains both named and anonymous parameters")
423
424 }
425
426
427
428
429 if variadic {
430 last := params[len(params)-1]
431 last.typ = &Slice{elem: last.typ}
432 check.recordTypeAndValue(list[len(list)-1].Type, typexpr, last.typ, nil)
433 }
434
435 return
436 }
437
438
439 func (check *Checker) declareParams(names []*syntax.Name, params []*Var, scopePos syntax.Pos) {
440 for i, name := range names {
441 if name != nil && name.Value != "" {
442 check.declare(check.scope, name, params[i], scopePos)
443 }
444 }
445 }
446
447
448
449 func (check *Checker) validRecv(pos poser, recv *Var) {
450
451 rtyp, _ := deref(recv.typ)
452 atyp := Unalias(rtyp)
453 if !isValid(atyp) {
454 return
455 }
456
457
458
459 switch T := atyp.(type) {
460 case *Named:
461 if T.obj.pkg != check.pkg || isCGoTypeObj(T.obj) {
462 check.errorf(pos, InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
463 break
464 }
465 var cause string
466 switch u := T.Underlying().(type) {
467 case *Basic:
468
469 if u.kind == UnsafePointer {
470 cause = "unsafe.Pointer"
471 }
472 case *Pointer, *Interface:
473 cause = "pointer or interface type"
474 case *TypeParam:
475
476
477 panic("unreachable")
478 }
479 if cause != "" {
480 check.errorf(pos, InvalidRecv, "invalid receiver type %s (%s)", rtyp, cause)
481 }
482 case *Basic:
483 check.errorf(pos, InvalidRecv, "cannot define new methods on non-local type %s", rtyp)
484 default:
485 check.errorf(pos, InvalidRecv, "invalid receiver type %s", recv.typ)
486 }
487 }
488
489
490 func isCGoTypeObj(obj *TypeName) bool {
491 return strings.HasPrefix(obj.name, "_Ctype_") ||
492 strings.HasPrefix(filepath.Base(obj.pos.FileBase().Filename()), "_cgo_")
493 }
494
View as plain text