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