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