forked from elliotchance/c2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enum.go
226 lines (198 loc) · 5.54 KB
/
enum.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
// This file contains transpiling for enums.
package transpiler
import (
"go/token"
"strconv"
goast "go/ast"
"github.com/elliotchance/c2go/ast"
"github.com/elliotchance/c2go/program"
"github.com/elliotchance/c2go/types"
"github.com/elliotchance/c2go/util"
)
// ctypeEnumValue generates a specific expression for values used by some
// constants in ctype.h. This is to get around an issue that the real values
// need to be evaulated by the compiler; which c2go does not yet do.
//
// TOOD: Ability to evaluate constant expressions at compile time
// https://github.com/elliotchance/c2go/issues/77
func ctypeEnumValue(value int, t token.Token) goast.Expr {
// Produces an expression like: ((1 << (0)) << 8)
return &goast.ParenExpr{
X: util.NewBinaryExpr(
&goast.ParenExpr{
X: util.NewBinaryExpr(
util.NewIntLit(1),
token.SHL,
util.NewIntLit(value),
"int",
false,
),
},
t,
util.NewIntLit(8),
"int",
false,
),
}
}
func transpileEnumConstantDecl(p *program.Program, n *ast.EnumConstantDecl) (
*goast.ValueSpec, []goast.Stmt, []goast.Stmt) {
var value goast.Expr = util.NewIdent("iota")
valueType := "int"
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}
// Special cases for linux ctype.h. See the description for the
// ctypeEnumValue() function.
switch n.Name {
case "_ISupper":
value = ctypeEnumValue(0, token.SHL) // "((1 << (0)) << 8)"
valueType = "uint16"
case "_ISlower":
value = ctypeEnumValue(1, token.SHL) // "((1 << (1)) << 8)"
valueType = "uint16"
case "_ISalpha":
value = ctypeEnumValue(2, token.SHL) // "((1 << (2)) << 8)"
valueType = "uint16"
case "_ISdigit":
value = ctypeEnumValue(3, token.SHL) // "((1 << (3)) << 8)"
valueType = "uint16"
case "_ISxdigit":
value = ctypeEnumValue(4, token.SHL) // "((1 << (4)) << 8)"
valueType = "uint16"
case "_ISspace":
value = ctypeEnumValue(5, token.SHL) // "((1 << (5)) << 8)"
valueType = "uint16"
case "_ISprint":
value = ctypeEnumValue(6, token.SHL) // "((1 << (6)) << 8)"
valueType = "uint16"
case "_ISgraph":
value = ctypeEnumValue(7, token.SHL) // "((1 << (7)) << 8)"
valueType = "uint16"
case "_ISblank":
value = ctypeEnumValue(8, token.SHR) // "((1 << (8)) >> 8)"
valueType = "uint16"
case "_IScntrl":
value = ctypeEnumValue(9, token.SHR) // "((1 << (9)) >> 8)"
valueType = "uint16"
case "_ISpunct":
value = ctypeEnumValue(10, token.SHR) // "((1 << (10)) >> 8)"
valueType = "uint16"
case "_ISalnum":
value = ctypeEnumValue(11, token.SHR) // "((1 << (11)) >> 8)"
valueType = "uint16"
default:
if len(n.Children()) > 0 {
var err error
value, _, preStmts, postStmts, err = transpileToExpr(n.Children()[0], p, false)
if err != nil {
panic(err)
}
}
}
return &goast.ValueSpec{
Names: []*goast.Ident{util.NewIdent(n.Name)},
Type: util.NewTypeIdent(valueType),
Values: []goast.Expr{value},
}, preStmts, postStmts
}
func transpileEnumDecl(p *program.Program, n *ast.EnumDecl) error {
preStmts := []goast.Stmt{}
postStmts := []goast.Stmt{}
// Enum without name is `const`
if n.Name == "" {
for _, c := range n.Children() {
e, newPre, newPost := transpileEnumConstantDecl(p, c.(*ast.EnumConstantDecl))
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
p.File.Decls = append(p.File.Decls, &goast.GenDecl{
Tok: token.CONST,
Specs: []goast.Spec{
e,
},
})
}
return nil
}
// Enums with names
theType, err := types.ResolveType(p, "int")
if err != nil {
// by defaults enum in C is INT
p.AddMessage(p.GenerateWarningMessage(err, n))
}
// Create alias of enum for int
p.File.Decls = append(p.File.Decls, &goast.GenDecl{
Tok: token.TYPE,
Specs: []goast.Spec{
&goast.TypeSpec{
Name: &goast.Ident{
Name: n.Name,
Obj: goast.NewObj(goast.Typ, n.Name),
},
Type: util.NewTypeIdent(theType),
},
},
})
// Registration new type in program.Program
if !p.IsTypeAlreadyDefined(n.Name) {
p.DefineType(n.Name)
}
baseType := util.NewTypeIdent(n.Name)
decl := &goast.GenDecl{
Tok: token.CONST,
}
// counter for replace iota
var counter int
for i, c := range n.Children() {
e, newPre, newPost := transpileEnumConstantDecl(p, c.(*ast.EnumConstantDecl))
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
e.Names[0].Obj = goast.NewObj(goast.Con, e.Names[0].Name)
if i > 0 {
e.Type = nil
e.Values = nil
}
if i == 0 {
e.Type = baseType
if t, ok := e.Type.(*goast.Ident); ok {
t.Obj = &goast.Object{
Name: n.Name,
Kind: goast.Typ,
Decl: &goast.TypeSpec{
Name: &goast.Ident{
Name: n.Name,
},
Type: &goast.Ident{
Name: "int", // enum in C is "INT" by default
},
},
}
}
}
if len(c.(*ast.EnumConstantDecl).ChildNodes) > 0 {
if integr, ok := c.(*ast.EnumConstantDecl).ChildNodes[0].(*ast.IntegerLiteral); ok {
is, err := strconv.ParseInt(integr.Value, 10, 64)
if err != nil {
p.AddMessage(p.GenerateWarningMessage(err, n))
}
counter = int(is)
}
}
// Insert value of constants
e.Values = []goast.Expr{
&goast.BasicLit{
Kind: token.INT,
Value: strconv.Itoa(counter),
},
}
// Position inside (....), it is
// not value of constants
e.Names[0].Obj.Data = i
counter++
decl.Specs = append(decl.Specs, e)
// registration of enum constants
p.EnumConstantToEnum[e.Names[0].Name] = "enum " + n.Name
}
// important value for creating (.....)
// with constants inside
decl.Lparen = 1
p.File.Decls = append(p.File.Decls, decl)
return nil
}