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