-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtype_tuple.go
More file actions
266 lines (240 loc) · 6.7 KB
/
type_tuple.go
File metadata and controls
266 lines (240 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
Copyright 2026 The XGo Authors (xgo.dev)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gogen
import (
"go/ast"
"go/token"
"go/types"
"strconv"
)
// ----------------------------------------------------------------------------
/*
// vFields defines the interface for virtual fields of a struct type.
type vFields interface { // virtual fields
FindField(cb *CodeBuilder, t *types.Struct, name string, arg *Element, src ast.Node) bool
FieldRef(cb *CodeBuilder, t *types.Struct, name string, x ast.Expr, src ast.Node) bool
}
*/
type vFields = *tupleFields
type vFieldsMgr struct {
vfts map[*types.Struct]vFields
}
func (p *CodeBuilder) refVField(t *types.Struct, name string, x ast.Expr, src ast.Node) bool {
if vft, ok := p.vfts[t]; ok {
return vft.FieldRef(p, t, name, x, src)
}
return false
}
func (p *CodeBuilder) findVField(t *types.Struct, name string, arg *Element, src ast.Node) bool {
if vft, ok := p.vfts[t]; ok {
return vft.FindField(p, t, name, arg, src)
}
return false
}
func (p *Package) setVFields(t *types.Struct, vft vFields) {
if p.cb.vfts == nil {
p.cb.vfts = make(map[*types.Struct]vFields)
}
p.cb.vfts[t] = vft
}
/*
func (p *Package) vFields(t *types.Struct) (vft vFields, ok bool) {
vft, ok = p.cb.vfts[t]
return
}
*/
// ----------------------------------------------------------------------------
type tupleFields struct {
fields []*types.Var
}
func (p *tupleFields) FindField(cb *CodeBuilder, t *types.Struct, name string, arg *Element, src ast.Node) bool {
for i, fld := range p.fields {
if fld.Name() == name {
cb.stk.Ret(1, &Element{
Val: selector(arg, tupleFieldName(i)),
Type: fld.Type(),
Src: src,
})
if cb.rec != nil {
cb.rec.Member(src, fld)
}
return true
}
}
return false
}
func (p *tupleFields) FieldRef(cb *CodeBuilder, t *types.Struct, name string, x ast.Expr, src ast.Node) bool {
for i, fld := range p.fields {
if fld.Name() == name {
if cb.rec != nil {
cb.rec.Member(src, fld)
}
ordName := tupleFieldName(i)
cb.stk.Ret(1, &Element{
Val: &ast.SelectorExpr{X: x, Sel: ident(ordName)},
Type: &refType{typ: fld.Type()},
})
return true
}
}
return false
}
// IsTupleType reports whether typ is a tuple type.
func (p *CodeBuilder) IsTupleType(typ types.Type) bool {
return checkTupleType(typ) != nil
}
/*
NOTE(xsw): Remove auto-unpacking (https://github.com/goplus/xgo/issues/2538)
func commaOK(typ types.Type, lhs int) bool {
if lhs < 2 {
return false
}
if lhs == 2 {
return true
}
if t := checkTupleType(typ); t != nil {
return lhs == t.NumFields()+1
}
return false
}
*/
func checkTupleType(typ types.Type) (result *types.Struct) {
result, _ = typ.Underlying().(*types.Struct)
if result != nil {
// Reject if: (1) empty struct, or (2) first field is not named X_0
// A tuple type must have at least one field named X_0, X_1, etc.
if result.NumFields() == 0 || result.Field(0).Name() != tupleField0 {
result = nil
}
}
return
}
/*
NOTE(xsw): Remove auto-unpacking (https://github.com/goplus/xgo/issues/2538)
func (p *CodeBuilder) tryUnpackTuple(lhs int) int {
e := p.stk.Get(-1)
typ := e.Type
typTuple, isTuple := typ.(*types.Tuple)
if isTuple {
if isTuple = typTuple.Len() == 2; isTuple {
if lhs == 2 {
return 1
}
typ = typTuple.At(0).Type()
}
}
if t := checkTupleType(typ); t != nil {
n := t.NumFields()
p.stk.PopN(1)
val := e.Val
nArg := 1
nRet := n
if isTuple {
nArg = 2
nRet++
} else {
if _, ok := val.(*ast.Ident); ok {
for i := 0; i < n; i++ {
p.stk.Push(e)
p.MemberVal(tupleFieldName(i))
}
return n
}
}
pkg := p.pkg
pkgType := pkg.Types
args := make([]*types.Var, nArg)
args[0] = types.NewParam(token.NoPos, pkgType, "v", typ)
result := make([]*types.Var, nRet)
for i := 0; i < n; i++ {
result[i] = types.NewParam(token.NoPos, pkgType, "", t.Field(i).Type())
}
if isTuple {
v2Type := typTuple.At(1).Type()
args[1] = types.NewParam(token.NoPos, pkgType, "v2", v2Type)
result[n] = types.NewParam(token.NoPos, pkgType, "", v2Type)
}
p.NewClosure(types.NewTuple(args...), types.NewTuple(result...), false).BodyStart(pkg)
for i := 0; i < n; i++ {
p.Val(args[0]).MemberVal(tupleFieldName(i))
}
if isTuple {
p.Val(args[1])
}
p.Return(nRet).End()
p.stk.Push(e)
p.Call(1)
}
return 1
}
*/
// LookupField looks up a field by name in the given struct type t.
// It returns the field index if found, or -1 if not found.
// It checks both the original fields and the virtual fields (e.g. tuple
// fields).
func (p *CodeBuilder) LookupField(t *types.Struct, name string) int {
if c := name[0]; c >= '0' && c <= '9' { // tuple: ordinal field
name = "X_" + name
}
for i, n := 0, t.NumFields(); i < n; i++ {
if fld := t.Field(i); fld.Name() == name {
return i
}
}
if vft, ok := p.vfts[t]; ok {
for i, fld := range vft.fields {
if fld.Name() == name {
return i
}
}
}
return -1
}
// TupleLit creates a tuple literal.
func (p *CodeBuilder) TupleLit(typ types.Type, arity int, src ...ast.Node) *CodeBuilder {
if typ == nil {
pkg := p.pkg
pkgTypes := pkg.Types
args := p.stk.GetArgs(arity)
flds := make([]*types.Var, arity)
for i := 0; i < arity; i++ {
fldt := types.Default(args[i].Type)
flds[i] = types.NewField(token.NoPos, pkgTypes, "", fldt, false)
}
typ = pkg.NewTuple(false, flds...)
}
return p.StructLit(typ, arity, false, src...)
}
// NewTuple creates a tuple type with the given fields.
// The fields are named as X_0, X_1, ...
// If withName is true, the original fields can also be accessed by their
// original names in addition to the ordinal names (X_0, X_1, ...).
// For example, a field named "x" can be accessed as both tuple.X_0 and
// tuple.x.
func (p *Package) NewTuple(withName bool, fields ...*types.Var) *types.Struct {
ordinals := make([]*types.Var, len(fields))
for i, fld := range fields {
name := tupleFieldName(i)
ordinals[i] = types.NewVar(fld.Pos(), fld.Pkg(), name, fld.Type())
}
ret := types.NewStruct(ordinals, nil)
if withName {
p.setVFields(ret, &tupleFields{fields})
}
return ret
}
const tupleField0 = "X_0"
func tupleFieldName(i int) string {
return "X_" + strconv.Itoa(i)
}
// ----------------------------------------------------------------------------