-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathoperators.go
292 lines (250 loc) · 7.49 KB
/
operators.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
// This file contains functions transpiling some general operator expressions.
// See binary.go and unary.go.
package transpiler
import (
"fmt"
"go/token"
goast "go/ast"
"github.com/elliotchance/c2go/ast"
"github.com/elliotchance/c2go/program"
"github.com/elliotchance/c2go/types"
"github.com/elliotchance/c2go/util"
)
// transpileConditionalOperator transpiles a conditional (also known as a
// ternary) operator:
//
// a ? b : c
//
// We cannot simply convert these to an "if" statement becuase they by inside
// another expression.
//
// Since Go does not support the ternary operator or inline "if" statements we
// use a closure to work the same way.
//
// It is also important to note that C only evaulates the "b" or "c" condition
// based on the result of "a" (from the above example).
func transpileConditionalOperator(n *ast.ConditionalOperator, p *program.Program) (
*goast.CallExpr, string, []goast.Stmt, []goast.Stmt, error) {
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}
a, aType, newPre, newPost, err := transpileToExpr(n.Children()[0], p, false)
if err != nil {
return nil, "", nil, nil, err
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
b, bType, newPre, newPost, err := transpileToExpr(n.Children()[1], p, false)
if err != nil {
return nil, "", nil, nil, err
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
c, cType, newPre, newPost, err := transpileToExpr(n.Children()[2], p, false)
if err != nil {
return nil, "", nil, nil, err
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
a, err = types.CastExpr(p, a, aType, "bool")
if err != nil {
return nil, "", nil, nil, err
}
// TODO: Here it is being assumed that the return type of the
// conditional operator is the type of the 'false' result. Things
// are a bit more complicated then that in C.
fmt.Println("WRONG HERE")
fmt.Printf("NODE = %#v\n", n)
fmt.Println(ast.Atos(n))
fmt.Printf("CH[1] = %#v\n", n.Children()[1])
fmt.Printf("CH[2] = %#v\n", n.Children()[2])
b, err = types.CastExpr(p, b, bType, cType)
if err != nil {
fmt.Println("err = ", err)
return nil, "", nil, nil, err
}
returnType, err := types.ResolveType(p, cType)
if err != nil {
return nil, "", nil, nil, err
}
return util.NewFuncClosure(
returnType,
&goast.IfStmt{
Cond: a,
Body: &goast.BlockStmt{
List: []goast.Stmt{
&goast.ReturnStmt{
Results: []goast.Expr{b},
},
},
},
Else: &goast.BlockStmt{
List: []goast.Stmt{
&goast.ReturnStmt{
Results: []goast.Expr{c},
},
},
},
},
), cType, preStmts, postStmts, nil
}
// transpileParenExpr transpiles an expression that is wrapped in parentheses.
// There is a special case where "(0)" is treated as a NULL (since that's what
// the macro expands to). We have to return the type as "null" since we don't
// know at this point what the NULL expression will be used in conjuction with.
func transpileParenExpr(n *ast.ParenExpr, p *program.Program) (
*goast.ParenExpr, string, []goast.Stmt, []goast.Stmt, error) {
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}
e, eType, newPre, newPost, err := transpileToExpr(n.Children()[0], p, false)
if err != nil {
return nil, "", nil, nil, err
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
r := &goast.ParenExpr{
X: e,
}
if types.IsNullExpr(r) {
eType = "null"
}
return r, eType, preStmts, postStmts, nil
}
func transpileCompoundAssignOperator(n *ast.CompoundAssignOperator, p *program.Program, exprIsStmt bool) (
goast.Expr, string, []goast.Stmt, []goast.Stmt, error) {
operator := getTokenForOperator(n.Opcode)
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}
right, rightType, newPre, newPost, err := transpileToExpr(n.Children()[1], p, false)
if err != nil {
return nil, "", nil, nil, err
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
// Construct code for computing compound assign operation to an union field
memberExpr, ok := n.Children()[0].(*ast.MemberExpr)
if ok {
ref := memberExpr.GetDeclRefExpr()
if ref != nil {
// Get operator by removing last char that is '=' (e.g.: += becomes +)
binaryOperation := n.Opcode
binaryOperation = binaryOperation[:(len(binaryOperation) - 1)]
// TODO: Is this duplicate code in unary.go?
union := p.GetStruct(ref.Type)
if union != nil && union.IsUnion {
attrType, err := types.ResolveType(p, ref.Type)
if err != nil {
p.AddMessage(p.GenerateWarningMessage(err, memberExpr))
}
// Method names
getterName := getFunctionNameForUnionGetter(ref.Name, attrType, memberExpr.Name)
setterName := getFunctionNameForUnionSetter(ref.Name, attrType, memberExpr.Name)
// Call-Expression argument
argLHS := util.NewCallExpr(getterName)
argOp := getTokenForOperator(binaryOperation)
argRHS := right
argValue := util.NewBinaryExpr(argLHS, argOp, argRHS, "interface{}", exprIsStmt)
// Make Go expression
resExpr := util.NewCallExpr(setterName, argValue)
return resExpr, "", preStmts, postStmts, nil
}
}
}
left, leftType, newPre, newPost, err := transpileToExpr(n.Children()[0], p, false)
if err != nil {
return nil, "", nil, nil, err
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
// The right hand argument of the shift left or shift right operators
// in Go must be unsigned integers. In C, shifting with a negative shift
// count is undefined behaviour (so we should be able to ignore that case).
// To handle this, cast the shift count to a uint64.
if operator == token.SHL_ASSIGN || operator == token.SHR_ASSIGN {
right, err = types.CastExpr(p, right, rightType, "unsigned long long")
p.AddMessage(p.GenerateWarningOrErrorMessage(err, n, right == nil))
if right == nil {
right = util.NewNil()
}
}
resolvedLeftType, err := types.ResolveType(p, leftType)
if err != nil {
p.AddMessage(p.GenerateWarningMessage(err, n))
}
return util.NewBinaryExpr(left, operator, right, resolvedLeftType, exprIsStmt),
"", preStmts, postStmts, nil
}
// getTokenForOperator returns the Go operator token for the provided C
// operator.
func getTokenForOperator(operator string) token.Token {
switch operator {
// Arithmetic
case "--":
return token.DEC
case "++":
return token.INC
case "+":
return token.ADD
case "-":
return token.SUB
case "*":
return token.MUL
case "/":
return token.QUO
case "%":
return token.REM
// Assignment
case "=":
return token.ASSIGN
case "+=":
return token.ADD_ASSIGN
case "-=":
return token.SUB_ASSIGN
case "*=":
return token.MUL_ASSIGN
case "/=":
return token.QUO_ASSIGN
case "%=":
return token.REM_ASSIGN
case "&=":
return token.AND_ASSIGN
case "|=":
return token.OR_ASSIGN
case "^=":
return token.XOR_ASSIGN
case "<<=":
return token.SHL_ASSIGN
case ">>=":
return token.SHR_ASSIGN
// Bitwise
case "&":
return token.AND
case "|":
return token.OR
case "~":
return token.XOR
case ">>":
return token.SHR
case "<<":
return token.SHL
case "^":
return token.XOR
// Comparison
case ">=":
return token.GEQ
case "<=":
return token.LEQ
case "<":
return token.LSS
case ">":
return token.GTR
case "!=":
return token.NEQ
case "==":
return token.EQL
// Logical
case "!":
return token.NOT
case "&&":
return token.LAND
case "||":
return token.LOR
// Other
case ",":
return token.COMMA
}
panic(fmt.Sprintf("unknown operator: %s", operator))
}