/
adapter_go1.22.go
130 lines (108 loc) · 3.15 KB
/
adapter_go1.22.go
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
//go:build go1.22 && !go1.23
// +build go1.22,!go1.23
package patch
import (
"go/constant"
"cmd/compile/internal/base"
"cmd/compile/internal/ir"
"cmd/compile/internal/reflectdata"
"cmd/compile/internal/typecheck"
"cmd/compile/internal/types"
"cmd/internal/src"
)
const goMajor = 1
const goMinor = 22
const genericTrapNeedsWorkaround = true
const closureMayBeEliminatedDueToIfConst = false
func forEachFunc(callback func(fn *ir.Func) bool) {
for _, fn := range typecheck.Target.Funcs {
if !callback(fn) {
return
}
}
}
// go 1.20 does not require type
func NewNilExpr(pos src.XPos, t *types.Type) *ir.NilExpr {
return ir.NewNilExpr(pos, t)
}
func AddFuncs(fn *ir.Func) {
typecheck.Target.Funcs = append(typecheck.Target.Funcs, fn)
}
func NewFunc(fpos, npos src.XPos, sym *types.Sym, typ *types.Type) *ir.Func {
return ir.NewFunc(fpos, npos, sym, typ)
}
func NewSignature(pkg *types.Pkg, recv *types.Field, tparams, params, results []*types.Field) *types.Type {
return types.NewSignature(recv, params, results)
}
func NewBasicLit(pos src.XPos, t *types.Type, val constant.Value) ir.Node {
return ir.NewBasicLit(pos, t, val)
}
// NOTE: []*types.Field instead of *types.Type(go1.21)
func takeAddrs(fn *ir.Func, t []*types.Field, nameOnly bool) ir.Expr {
if len(t) == 0 {
return NewNilExpr(fn.Pos(), intfSlice)
}
paramList := make([]ir.Node, len(t))
i := 0
ForEachField(t, func(field *types.Field) bool {
paramList[i] = takeAddr(fn, field, nameOnly)
i++
return true
})
return ir.NewCompLitExpr(fn.Pos(), ir.OCOMPLIT, intfSlice, paramList)
}
// NOTE: []*types.Field instead of *types.Type(go1.21)
func getFieldNames(pos src.XPos, fn *ir.Func, t []*types.Field) ir.Expr {
if len(t) == 0 {
return NewNilExpr(pos, strSlice)
}
paramList := make([]ir.Node, len(t))
i := 0
ForEachField(t, func(field *types.Field) bool {
fieldName := getFieldName(fn, field)
paramList[i] = NewStringLit(pos, fieldName)
i++
return true
})
return ir.NewCompLitExpr(pos, ir.OCOMPLIT, strSlice, paramList)
}
func getTypeNames(params []*types.Field) []ir.Node {
paramNames := make([]ir.Node, 0, len(params))
for _, p := range params {
paramNames = append(paramNames, p.Nname.(*ir.Name))
}
return paramNames
}
func ForEachField(params []*types.Field, callback func(field *types.Field) bool) {
n := len(params)
for i := 0; i < n; i++ {
if !callback(params[i]) {
return
}
}
}
func GetFieldIndex(fields []*types.Field, i int) *types.Field {
return fields[i]
}
func getCallee(callNode *ir.CallExpr) ir.Node {
return callNode.Fun
}
// TODO: maybe go1.22 does not need this
func SetConvTypeWordPtr(conv *ir.ConvExpr, t *types.Type) {
conv.TypeWord = reflectdata.TypePtrAt(base.Pos, types.NewPtr(t))
}
func SetConvTypeWord(conv *ir.ConvExpr, t *types.Type) {
conv.TypeWord = reflectdata.TypePtrAt(base.Pos, t)
}
func getFuncResultsType(funcType *types.Type) *types.Type {
panic("getFuncResultsType should not be called above go1.19")
}
func canInsertTrap(fn *ir.Func) bool {
return true
}
func NewNameAt(pos src.XPos, sym *types.Sym, typ *types.Type) *ir.Name {
return ir.NewNameAt(pos, sym, typ)
}
func isClosureWrapperForGeneric(fn *ir.Func) bool {
return false
}