-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcasts.go
321 lines (318 loc) · 8.09 KB
/
casts.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package cxgo
import (
"fmt"
"math"
"github.com/gotranspile/cxgo/types"
)
func (g *translator) cCast(toType types.Type, x Expr) Expr {
if toType == nil {
panic("no type")
}
toKind := toType.Kind()
xType := x.CType(toType)
xKind := xType.Kind()
if xType == g.env.Go().Any() {
return &CCastExpr{Assert: true, Type: toType, Expr: x}
}
if toType == g.env.Go().Any() {
return x
}
if at, ok := toType.(types.ArrayType); ok && at.IsSlice() {
switch x := x.(type) {
case Nil:
return g.Nil()
case IntLit:
if x.IsZero() {
return g.Nil()
}
case *TakeAddr:
if ind, ok := x.X.(*CIndexExpr); ok && types.Same(ind.Expr.CType(nil), toType) {
if ind.IndexZero() {
// special case: unwrap unnecessary cast to slice
return ind.Expr
}
}
}
if fc, ok := x.(*CallExpr); ok && len(fc.Args) >= 1 {
if f, ok := fc.Fun.(Ident); ok {
gg := g.env.Go()
switch f.Identifier() {
case gg.SliceFunc(),
gg.AppendFunc():
if types.Same(toType, fc.Args[0].CType(nil)) {
return x
}
}
}
}
}
if xKind.Is(types.Array) && !toKind.Is(types.Array) {
x = g.cAddr(x)
return g.cCast(toType, x)
}
// equal or same type: no conversion
if types.Same(toType, xType) {
return x
}
// unknown types: bypass
if toKind.Is(types.Unknown) {
// special cases for well-known types
switch toType {
case g.env.Go().String():
var fnc *types.Ident
if types.Same(xType, g.env.C().String()) {
fnc = g.env.StringC2Go()
} else if types.Same(xType, g.env.C().WString()) {
fnc = g.env.WStringC2Go()
} else {
return g.cCast(g.env.C().String(), x)
}
return g.NewCCallExpr(FuncIdent{fnc}, []Expr{x})
}
return x
}
if c1, ok := cUnwrap(x).(*CCastExpr); ok {
// casts A(A(x)) -> A(x)
if types.Same(c1.Type, toType) {
return c1
}
}
// conversions to bool - we have a specialized function for that
if toKind.IsBool() {
return g.ToBool(x)
}
// nil should be first, because it's an "untyped ptr"
if xKind.Is(types.Nil) {
if toKind.IsPtr() || toKind.IsFunc() {
return cUnwrap(x)
}
}
// strings are immutable, so call a specialized function for conversion
if types.Same(xType, g.env.Go().String()) {
// string -> []byte
if at, ok := types.Unwrap(toType).(types.ArrayType); ok && at.IsSlice() && at.Elem() == g.env.Go().Byte() {
return &CCastExpr{Type: at, Expr: x}
}
// [N]byte = "xyz"
if at, ok := types.Unwrap(toType).(types.ArrayType); ok && (types.Same(at.Elem(), g.env.Go().Byte()) || xKind == types.Unknown) {
if !at.IsSlice() {
tmp := types.NewIdent("t", at)
copyF := FuncIdent{g.env.Go().CopyFunc()}
// declare a function literal returning an array of the same size
var body []CStmt
// declare temp variable with an array type
body = append(body, g.NewCDeclStmt(&CVarDecl{CVarSpec: CVarSpec{
g: g,
Type: at,
Names: []*types.Ident{tmp},
}})...)
body = append(body,
// copy string into it
&CExprStmt{Expr: &CallExpr{
Fun: copyF, Args: []Expr{
&SliceExpr{Expr: IdentExpr{tmp}}, // tmp[:]
&CCastExpr{Type: types.SliceT(at.Elem()), Expr: x}, // ([]TYPE)("xyz")
},
}},
)
// return temp variable
body = append(body, g.NewReturnStmt(IdentExpr{tmp}, at)...)
lit := g.NewFuncLit(g.env.FuncTT(at), body...)
return g.NewCCallExpr(lit, nil)
}
}
if types.Same(toType, g.env.C().WString()) {
return g.cCast(toType, g.NewCCallExpr(FuncIdent{g.env.WStringGo2C()}, []Expr{x}))
}
return g.cCast(toType, g.NewCCallExpr(FuncIdent{g.env.StringGo2C()}, []Expr{x}))
}
if xType == g.env.Go().String() {
var conv *types.Ident
if types.Same(toType, g.env.C().String()) {
conv = g.env.StringGo2C()
} else if types.Same(toType, g.env.C().WString()) {
conv = g.env.WStringGo2C()
}
if conv != nil {
return g.cCast(toType, g.NewCCallExpr(FuncIdent{conv}, []Expr{x}))
}
}
// any casts from array to other types should go through pointer to an array
if xKind.Is(types.Unknown) {
return &CCastExpr{
Type: toType,
Expr: x,
}
}
switch {
case toKind.IsPtr():
return g.cPtrToPtr(toType, g.ToPointer(x))
case toKind.IsInt():
if l, ok := cUnwrap(x).(IntLit); ok {
ti, ok := types.Unwrap(toType).(types.IntType)
if l.IsUint() && ok && ti.Signed() && !litCanStore(ti, l) {
// try overflowing it
return l.OverflowInt(ti.Sizeof())
}
if l.IsUint() || (ok && ti.Signed()) {
return &CCastExpr{
Type: toType,
Expr: x,
}
}
sz := toType.Sizeof()
var uv uint64
switch sz {
case 1:
uv = math.MaxUint8
case 2:
uv = math.MaxUint16
case 4:
uv = math.MaxUint32
case 8:
uv = math.MaxUint64
default:
return &CCastExpr{
Type: toType,
Expr: x,
}
}
uv -= uint64(-l.Int()) - 1
return cUintLit(uv, l.base)
}
if xKind.IsFunc() {
// func() -> int
return &FuncToInt{
X: g.ToFunc(x, nil),
To: types.Unwrap(toType).(types.IntType),
}
}
if xKind.IsPtr() {
// *some -> int
return g.cPtrToInt(toType, g.ToPointer(x))
}
if x.IsConst() && xKind.IsUntypedInt() {
return x
}
if xKind.IsBool() {
return g.cCast(toType, &BoolToInt{X: g.ToBool(x)})
}
xi, ok1 := types.Unwrap(xType).(types.IntType)
ti, ok2 := types.Unwrap(toType).(types.IntType)
if ok1 && ok2 && xi.Signed() != ti.Signed() {
if ti.Sizeof() > xi.Sizeof() {
return &CCastExpr{
Type: toType,
Expr: x,
}
} else if ti.Sizeof() < xi.Sizeof() {
var t2 types.Type
if !ti.Signed() {
t2 = types.IntT(ti.Sizeof())
} else {
t2 = types.UintT(ti.Sizeof())
}
return &CCastExpr{
Type: toType,
Expr: g.cCast(t2, x),
}
}
}
return &CCastExpr{
Type: toType,
Expr: x,
}
case toKind.IsFunc():
switch x := cUnwrap(x).(type) {
case Nil:
return x
case IntLit:
if x.IsZero() {
return g.Nil()
}
}
if !xKind.IsFunc() {
x = g.ToFunc(x, types.Unwrap(toType).(*types.FuncType))
return g.cCast(toType, x)
}
ft, fx := types.Unwrap(toType).(*types.FuncType), types.Unwrap(xType).(*types.FuncType)
if (ft.Variadic() == fx.Variadic() || !ft.Variadic()) && ft.ArgN() >= fx.ArgN() && ((ft.Return() != nil) == (fx.Return() != nil) || (ft.Return() == nil && fx.Return() != nil)) {
// cannot cast directly, but can return lambda instead
callArgs := make([]Expr, 0, ft.ArgN())
funcArgs := make([]*types.Field, 0, ft.ArgN())
for i, a := range ft.Args() {
at := a.Type()
name := types.NewIdent(fmt.Sprintf("arg%d", i+1), at)
if a.Name != nil && !a.Name.IsUnnamed() {
name = types.NewIdent(a.Name.Name, at)
}
funcArgs = append(funcArgs, &types.Field{
Name: name,
})
callArgs = append(callArgs, IdentExpr{name})
}
argn := fx.ArgN()
if ft.Variadic() {
callArgs = append(callArgs, &ExpandExpr{X: IdentExpr{types.NewIdent("_rest", types.UnkT(1))}})
argn++
}
e := g.NewCCallExpr(g.ToFunc(x, nil), callArgs[:argn])
var stmts []CStmt
if ft.Return() != nil {
stmts = g.NewReturnStmt(e, ft.Return())
} else {
stmts = NewCExprStmt(e)
}
var litT *types.FuncType
if ft.Variadic() {
litT = g.env.VarFuncT(
ft.Return(),
funcArgs...,
)
} else {
litT = g.env.FuncT(
ft.Return(),
funcArgs...,
)
}
return g.NewFuncLit(litT, stmts...)
}
// incompatible function types - force error
return x
case toKind.IsFloat():
if xKind.IsUntypedFloat() {
return x
}
if xKind.IsUntypedInt() {
if !toKind.IsUntypedFloat() {
toType = types.AsUntypedFloatT(types.Unwrap(toType).(types.FloatType))
toKind = toType.Kind()
}
}
if xKind.IsBool() {
return g.cCast(toType, &BoolToInt{X: g.ToBool(x)})
}
case toKind.Is(types.Array):
ta := types.Unwrap(toType).(types.ArrayType)
if xa, ok := types.Unwrap(xType).(types.ArrayType); ok && ta.Len() == 0 && xa.Len() != 0 {
return &SliceExpr{Expr: x}
}
case toKind.Is(types.Struct):
isZero := false
switch x := cUnwrap(x).(type) {
case Nil:
isZero = true
case IntLit:
if x.IsZero() {
isZero = true
}
}
if isZero {
return g.NewCCompLitExpr(toType, nil)
}
}
return &CCastExpr{
Type: toType,
Expr: x,
}
}