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