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