-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
translate.go
396 lines (364 loc) · 10.4 KB
/
translate.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package cxgo
import (
"bytes"
"fmt"
"go/format"
"os"
"path/filepath"
"regexp"
"strings"
"modernc.org/cc/v3"
"modernc.org/token"
"github.com/gotranspile/cxgo/libs"
"github.com/gotranspile/cxgo/types"
)
type Config struct {
Root string
Package string
GoFile string
GoFilePref string
Include []string
SysInclude []string
IncludeMap map[string]string
MaxDecls int
Predef string
Define []Define
FlattenAll bool
ForwardDecl bool
SkipDecl map[string]bool
Idents []IdentConfig
Replace []Replacer
Hooks bool
FixImplicitReturns bool
IgnoreIncludeDir bool
UnexportedFields bool // do not export struct fields for Go
IntReformat bool // automatically select new base for formatting int literals
KeepFree bool // do not rewrite free() calls to nil assignments
DoNotEdit bool // generate DO NOT EDIT header comments
}
type TypeHint string
const (
HintBool = TypeHint("bool") // force the type to Go bool
HintSlice = TypeHint("slice") // force type to Go slice (for pointers and arrays)
HintIface = TypeHint("iface") // force type to Go interface{}
HintString = TypeHint("string") // force type to Go string
)
type IdentConfig struct {
Name string `yaml:"name" json:"name"` // identifier name in C
Index int `yaml:"index" json:"index"` // argument index, only for Fields in the function decl
Rename string `yaml:"rename" json:"rename"` // rename the identifier
Alias bool `yaml:"alias" json:"alias"` // omit declaration, use underlying type instead
Type TypeHint `yaml:"type" json:"type"` // changes the Go type of this identifier
Flatten *bool `yaml:"flatten" json:"flatten"` // flattens function control flow to workaround invalid gotos
Fields []IdentConfig `yaml:"fields" json:"fields"` // configs for struct fields or func arguments
}
type Replacer struct {
Old string
Re *regexp.Regexp
New string
}
type FileError struct {
Err error
Where token.Position
}
func (e *FileError) Unwrap() error {
return e.Err
}
func (e *FileError) Error() string {
return e.Where.String() + ": " + e.Err.Error()
}
func ErrorWithPos(err error, where token.Position) error {
if err == nil {
return nil
}
if e, ok := err.(*FileError); ok {
return e
}
return &FileError{Err: err, Where: where}
}
func ErrorfWithPos(where token.Position, format string, args ...any) error {
return ErrorWithPos(fmt.Errorf(format, args...), where)
}
func Translate(root, fname, out string, env *libs.Env, conf Config) error {
cname := fname
tu, err := Parse(env, root, cname, SourceConfig{
Predef: conf.Predef,
Define: conf.Define,
Include: conf.Include,
SysInclude: conf.SysInclude,
IgnoreIncludeDir: conf.IgnoreIncludeDir,
})
if err != nil {
return fmt.Errorf("parsing failed: %w", err)
}
decls, err := TranslateAST(cname, tu, env, conf)
if err != nil {
return err
}
pkg := conf.Package
if pkg == "" {
pkg = "lib"
}
_ = os.MkdirAll(out, 0755)
bbuf := bytes.NewBuffer(nil)
gofile := conf.GoFile
if gofile == "" {
gofile, err = filepath.Rel(root, fname)
if err != nil {
return err
}
if conf.GoFilePref != "" {
dir, base := filepath.Split(gofile)
gofile = dir + conf.GoFilePref + base
}
// flatten C source file path to make a single large Go package
// TODO: auto-generate Go packages based on dir structure
gofile = strings.ReplaceAll(gofile, string(filepath.Separator), "_")
gofile = strings.TrimSuffix(gofile, ".c")
gofile = strings.TrimSuffix(gofile, ".h")
gofile += ".go"
}
max := conf.MaxDecls
if max == 0 {
max = 100
}
// optionally split large files by N declaration per file
for i := 0; len(decls) > 0; i++ {
cur := decls
if max > 0 && len(cur) > max {
cur = cur[:max]
}
decls = decls[len(cur):]
// generate Go file header with a package name and a list of imports
header := ImportsFor(env, cur)
buf := make([]GoDecl, 0, len(header)+len(cur))
buf = append(buf, header...)
buf = append(buf, cur...)
bbuf.Reset()
err = PrintGo(bbuf, pkg, buf, conf.DoNotEdit)
if err != nil {
return err
}
suff := fmt.Sprintf("_p%d", i+1)
if i == 0 && len(decls) == 0 {
suff = ""
}
gopath := strings.TrimSuffix(gofile, ".go") + suff + ".go"
if !filepath.IsAbs(gopath) {
gopath = filepath.Join(out, gopath)
}
fdata := bbuf.Bytes()
// run replacements defined in the config
for _, rep := range conf.Replace {
if rep.Re != nil {
fdata = rep.Re.ReplaceAll(fdata, []byte(rep.New))
} else {
fdata = bytes.ReplaceAll(fdata, []byte(rep.Old), []byte(rep.New))
}
}
fmtdata, err := format.Source(fdata)
if err != nil {
// write anyway for examination
_ = os.WriteFile(gopath, fdata, 0644)
return fmt.Errorf("error formatting %s: %v", filepath.Base(gofile), err)
}
err = os.WriteFile(gopath, fmtdata, 0644)
if err != nil {
return err
}
}
return nil
}
// TranslateAST takes a C translation unit and converts it to a list of Go declarations.
func TranslateAST(fname string, tu *cc.AST, env *libs.Env, conf Config) ([]GoDecl, error) {
t := newTranslator(env, conf)
return t.translate(fname, tu), nil
}
// TranslateCAST takes a C translation unit and converts it to a list of cxgo declarations.
func TranslateCAST(fname string, tu *cc.AST, env *libs.Env, conf Config) ([]CDecl, error) {
t := newTranslator(env, conf)
return t.translateC(fname, tu), nil
}
func newTranslator(env *libs.Env, conf Config) *translator {
tr := &translator{
env: env,
tenv: env.Clone(),
conf: conf,
idents: make(map[string]IdentConfig),
ctypes: make(map[cc.Type]types.Type),
decls: make(map[cc.Node]*types.Ident),
namedPtrs: make(map[string]types.PtrType),
named: make(map[string]types.Named),
aliases: make(map[string]types.Type),
macros: make(map[string]*types.Ident),
}
for _, v := range conf.Idents {
tr.idents[v.Name] = v
}
_, _ = tr.tenv.GetLibrary(libs.BuiltinH)
_, _ = tr.tenv.GetLibrary(libs.StdlibH)
_, _ = tr.tenv.GetLibrary(libs.StdioH)
return tr
}
type translator struct {
env *libs.Env
tenv *libs.Env // virtual env for stdlib forward declarations
conf Config
file *cc.AST
cur string
idents map[string]IdentConfig
ctypes map[cc.Type]types.Type
namedPtrs map[string]types.PtrType
named map[string]types.Named
aliases map[string]types.Type
macros map[string]*types.Ident
decls map[cc.Node]*types.Ident
}
func (g *translator) Nil() Nil {
return NewNil(g.env.PtrSize())
}
func (g *translator) Iota() Expr {
return IdentExpr{g.env.Go().Iota()}
}
const (
libcCStringSliceName = "libc.CStringSlice"
)
func (g *translator) translateMain(d *CFuncDecl) {
osExit := g.env.Go().OsExitFunc()
if d.Type.ArgN() == 2 {
libcCSlice := types.NewIdent(libcCStringSliceName, g.env.FuncTT(g.env.PtrT(g.env.C().String()), types.SliceT(g.env.Go().String())))
osArgs := types.NewIdent("os.Args", types.SliceT(g.env.Go().String()))
argsLen := &CallExpr{Fun: FuncIdent{g.env.Go().LenFunc()}, Args: []Expr{IdentExpr{osArgs}}}
argsPtr := &CallExpr{Fun: FuncIdent{libcCSlice}, Args: []Expr{IdentExpr{osArgs}}}
// define main args in the function body
args := d.Type.Args()
argc := g.NewCDeclStmt(&CVarDecl{CVarSpec: CVarSpec{
g: g,
Type: args[0].Type(),
Names: []*types.Ident{args[0].Name},
Inits: []Expr{g.cCast(args[0].Type(), argsLen)},
}})
argv := g.NewCDeclStmt(&CVarDecl{CVarSpec: CVarSpec{
g: g,
Type: args[1].Type(),
Names: []*types.Ident{args[1].Name},
Inits: []Expr{g.cCast(args[1].Type(), argsPtr)},
}})
var stmts []CStmt
stmts = append(stmts, argc...)
stmts = append(stmts, argv...)
stmts = append(stmts, d.Body.Stmts...)
d.Body.Stmts = stmts
d.Type = g.env.FuncT(d.Type.Return())
}
d.Body.Stmts, _ = cReplaceEachStmt(func(s CStmt) ([]CStmt, bool) {
r, ok := s.(*CReturnStmt)
if !ok {
return []CStmt{s}, false
}
e := r.Expr
if e == nil {
e = cIntLit(0, 10)
}
ex := g.NewCCallExpr(FuncIdent{osExit}, []Expr{g.cCast(g.env.Go().Int(), e)})
return NewCExprStmt(ex), true
}, d.Body.Stmts)
d.Type = g.env.FuncT(nil, d.Type.Args()...)
}
func (g *translator) translate(cur string, ast *cc.AST) []GoDecl {
decl := g.translateC(cur, ast)
g.rewriteStatements(decl)
if g.conf.FixImplicitReturns {
g.fixImplicitReturns(decl)
}
// adapt well-known decls like main
decl = g.adaptMain(decl)
// run plugin hooks
decl = g.runASTPluginsC(cur, ast, decl)
// flatten functions, if needed
g.flatten(decl)
// fix unused variables
g.fixUnusedVars(decl)
// convert to Go AST
var gdecl []GoDecl
for _, d := range decl {
switch d := d.(type) {
case *CFuncDecl:
if g.conf.SkipDecl[d.Name.Name] {
continue
}
case *CVarDecl:
// TODO: skip any single one
if len(d.Names) == 1 && g.conf.SkipDecl[d.Names[0].Name] {
continue
}
case *CTypeDef:
if g.conf.SkipDecl[d.Name().Name] {
continue
}
}
gdecl = append(gdecl, d.AsDecl()...)
}
return gdecl
}
func (g *translator) translateC(cur string, ast *cc.AST) []CDecl {
g.file, g.cur = ast, strings.TrimLeft(cur, "./")
decl := g.convertMacros(ast)
tu := ast.TranslationUnit
for tu != nil {
d := tu.ExternalDeclaration
tu = tu.TranslationUnit
if d == nil {
continue
}
var cd []CDecl
switch d.Case {
case cc.ExternalDeclarationFuncDef:
cd = g.convertFuncDef(d.FunctionDefinition)
case cc.ExternalDeclarationDecl:
cd = g.convertDecl(d.Declaration)
case cc.ExternalDeclarationEmpty:
// TODO
default:
panic(d.Case.String() + " " + d.Position().String())
}
decl = append(decl, cd...)
}
// remove forward declarations
m := make(map[string]CDecl)
skip := make(map[CDecl]struct{})
for _, d := range decl {
switch d := d.(type) {
case *CFuncDecl:
d2, ok := m[d.Name.Name].(*CFuncDecl)
if !ok {
m[d.Name.Name] = d
continue
}
if d2.Body != nil {
skip[d] = struct{}{}
} else {
m[d.Name.Name] = d
skip[d2] = struct{}{}
}
case *CTypeDef:
d2, ok := m[d.Name().Name].(*CTypeDef)
if !ok {
m[d.Name().Name] = d
continue
}
if d.Underlying() == d2.Underlying() {
m[d.Name().Name] = d
skip[d] = struct{}{}
}
}
}
decl2 := make([]CDecl, 0, len(decl))
for _, d := range decl {
if _, skip := skip[d]; skip {
continue
}
decl2 = append(decl2, d)
}
return decl2
}