-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen.go
590 lines (543 loc) · 12.9 KB
/
gen.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package main
import (
"bytes"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"io"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"golang.org/x/tools/imports"
"github.com/pkg/errors"
)
type empty struct{}
type genericContext struct {
fset *token.FileSet
genericTypes map[string]*Type
types map[token.Pos]ast.Spec
isGeneric map[token.Pos]bool
funcs map[token.Pos]ast.Decl
vars map[token.Pos]ast.Spec
consts map[token.Pos]ast.Spec
renamer *strings.Replacer
renamePairs []string
renames map[token.Pos]ast.Expr //*ast.SelectorExpr or *ast.Ident or *ast.StarExpr
visited map[token.Pos]bool
}
func lowerFirst(s string) string {
if s == "" {
return ""
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToLower(r)) + s[n:]
}
func upperFirst(s string) string {
if s == "" {
return ""
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToUpper(r)) + s[n:]
}
func (gctx *genericContext) registerGenericType(node ast.Decl) bool {
decl, ok := node.(*ast.GenDecl)
if !ok {
return false
}
for _, spec := range decl.Specs {
ts, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
if ts.Name == nil {
continue
}
gType, ok := gctx.genericTypes[ts.Name.String()]
if !ok {
continue
}
gctx.types[ts.Pos()] = ts
gctx.isGeneric[ts.Pos()] = true
gctx.visited[ts.Pos()] = true
if gType.PkgName != "" {
gctx.renames[ts.Pos()] = &ast.SelectorExpr{
X: &ast.Ident{
Name: gType.PkgName,
},
Sel: &ast.Ident{
Name: gType.Name,
},
}
} else {
gctx.renames[ts.Pos()] = &ast.Ident{
Name: gType.Name,
}
}
if gType.Pointer {
gctx.renames[ts.Pos()] = &ast.StarExpr{
X: gctx.renames[ts.Pos()],
}
}
gctx.renamePairs = append(gctx.renamePairs,
lowerFirst(ts.Name.String()), lowerFirst(gType.Name),
upperFirst(ts.Name.String()), upperFirst(gType.Name),
)
return true
}
return false
}
func (gctx *genericContext) registerGenericTypes(file *ast.File) {
gctx.renamePairs = make([]string, 0)
for _, decl := range file.Decls {
gctx.registerGenericType(decl)
}
gctx.renamer = strings.NewReplacer(gctx.renamePairs...)
}
func (gctx *genericContext) isDependant(node ast.Node) bool {
found := false
// Methods on the generic types are like interfaces,
// they should not be reified.
if funcDecl, ok := node.(*ast.FuncDecl); ok {
if funcDecl.Recv != nil {
for _, field := range funcDecl.Recv.List {
// Ident or StarExpr
var i *ast.Ident
switch x := field.Type.(type) {
case *ast.StarExpr:
i = x.X.(*ast.Ident)
case *ast.Ident:
i = x
}
if spec, ok := i.Obj.Decl.(*ast.TypeSpec); ok {
if gctx.isGeneric[spec.Pos()] {
return false
}
}
}
}
}
ast.Inspect(node, func(n ast.Node) bool {
if n, ok := n.(*ast.Ident); ok && n.Obj != nil {
// check types
if spec, ok := n.Obj.Decl.(*ast.TypeSpec); ok {
for pos := range gctx.types {
if spec.Pos() == pos {
found = true
return false // no need to check children
}
}
}
// check functions
if fdecl, ok := n.Obj.Decl.(*ast.FuncDecl); ok {
for pos := range gctx.funcs {
if fdecl.Pos() == pos {
found = true
return false // no need to check children
}
}
}
// check variables
if spec, ok := n.Obj.Decl.(*ast.ValueSpec); ok {
for pos := range gctx.vars {
if spec.Pos() == pos {
found = true
return false // no need to check children
}
}
for pos := range gctx.consts {
if spec.Pos() == pos {
found = true
return false // no need to check children
}
}
}
}
return true
})
return found
}
func (gctx *genericContext) addDependant(n ast.Node, isConst bool) {
switch d := n.(type) {
case *ast.FuncDecl:
gctx.funcs[d.Pos()] = d
if d.Recv != nil {
// If a method depends on a generic type,
// the entire struct and all methods must be
// specialized.
for _, field := range d.Recv.List {
// Ident or StarExpr
var i *ast.Ident
switch x := field.Type.(type) {
case *ast.StarExpr:
i = x.X.(*ast.Ident)
case *ast.Ident:
i = x
}
gctx.addDependant(i.Obj.Decl.(ast.Node), false)
}
} else {
// If it's not a method, it must be renamed.
if d.Name != nil {
gctx.renames[d.Pos()] = &ast.Ident{
Name: gctx.renamer.Replace(d.Name.Name),
}
}
}
case *ast.TypeSpec:
gctx.types[d.Pos()] = d
if d.Name != nil {
gctx.renames[d.Pos()] = &ast.Ident{
Name: gctx.renamer.Replace(d.Name.Name),
}
}
case *ast.ValueSpec:
if isConst {
gctx.consts[d.Pos()] = d
} else {
gctx.vars[d.Pos()] = d
}
for _, name := range d.Names {
if name != nil {
gctx.renames[name.Pos()] = &ast.Ident{
Name: gctx.renamer.Replace(name.Name),
}
}
}
default:
panic("invalid decl")
}
gctx.visited[n.Pos()] = true
}
func (gctx *genericContext) collectDependants(file *ast.File) {
changed := true
type nodeData struct {
n ast.Node
isConst bool
}
var nodes []nodeData
for _, decl := range file.Decls {
switch d := decl.(type) {
case *ast.FuncDecl:
nodes = append(nodes, nodeData{
n: d,
})
case *ast.GenDecl:
isConst := false
if d.Tok == token.CONST {
isConst = true
}
for _, s := range d.Specs {
switch s := s.(type) {
case *ast.ImportSpec:
if s.Doc == nil {
s.Doc = d.Doc
}
case *ast.TypeSpec:
if s.Doc == nil {
s.Doc = d.Doc
}
case *ast.ValueSpec:
if s.Doc == nil {
s.Doc = d.Doc
}
}
nodes = append(nodes, nodeData{
n: s,
isConst: isConst,
})
}
}
}
for changed {
changed = false
for _, node := range nodes {
if gctx.visited[node.n.Pos()] {
continue
}
if gctx.isDependant(node.n) {
changed = true
gctx.addDependant(node.n, node.isConst)
}
}
}
}
func (gctx *genericContext) doRenames(n ast.Node) {
/*
fmt.Printf("Renames:\n")
for k, v := range gctx.renames {
fmt.Printf("%v: %#v\n", gctx.fset.Position(k).String(), v)
}
fmt.Printf("\n\n")
*/
// We can't touch the file until we've finished analyzing it,
// because replacing a declaration with the renamed declaration
// will change the original declaration's pos in the Ident's Obj.Decl field.
type renameJob struct {
parent ast.Node
name string
index int
replacement ast.Node
}
renames := make([]*renameJob, 0)
Apply(n, func(parent ast.Node, name string, index int, n ast.Node) bool {
if n, ok := n.(*ast.Ident); ok && n != nil && n.Obj != nil {
if gctx.isGeneric[n.Pos()] {
// Skip the generic type declaration.
return false
}
// check types
if spec, ok := n.Obj.Decl.(*ast.TypeSpec); ok {
if renameTo, ok := gctx.renames[spec.Pos()]; ok {
renames = append(renames, &renameJob{
parent: parent,
name: name,
index: index,
replacement: renameTo,
})
}
}
// check functions
if fdecl, ok := n.Obj.Decl.(*ast.FuncDecl); ok {
if renameTo, ok := gctx.renames[fdecl.Pos()]; ok {
renames = append(renames, &renameJob{
parent: parent,
name: name,
index: index,
replacement: renameTo,
})
}
}
// check variables
if spec, ok := n.Obj.Decl.(*ast.ValueSpec); ok {
if parent.Pos() == spec.Pos() {
if renameTo, ok := gctx.renames[n.Pos()]; ok {
renames = append(renames, &renameJob{
parent: parent,
name: name,
index: index,
replacement: renameTo,
})
}
} else {
for _, ident := range spec.Names {
if renameTo, ok := gctx.renames[ident.Pos()]; ok && ident.Name == n.Name {
renames = append(renames, &renameJob{
parent: parent,
name: name,
index: index,
replacement: renameTo,
})
}
}
}
}
}
return true
}, nil)
for _, job := range renames {
SetField(job.parent, job.name, job.index, job.replacement)
}
}
func (gctx *genericContext) renameComments(cg *ast.CommentGroup) *ast.CommentGroup {
if cg == nil {
return cg
}
for _, c := range cg.List {
c.Text = gctx.renamer.Replace(c.Text)
}
return cg
}
func gen(in io.Reader, inFilename string, targetPackageName string, typeMapping map[string]*Type, out io.Writer, outFilename string) error {
gctx := &genericContext{
fset: token.NewFileSet(),
genericTypes: typeMapping,
types: make(map[token.Pos]ast.Spec),
isGeneric: make(map[token.Pos]bool),
funcs: make(map[token.Pos]ast.Decl),
vars: make(map[token.Pos]ast.Spec),
consts: make(map[token.Pos]ast.Spec),
visited: make(map[token.Pos]bool),
renames: make(map[token.Pos]ast.Expr),
}
file, err := parser.ParseFile(gctx.fset, inFilename, in, parser.ParseComments)
if err != nil {
return errors.Wrap(err, "parsing file failed")
}
// ast.Print(gctx.fset, file)
// TODO: multiple files
outImports := make([]*ast.ImportSpec, 0)
for _, gType := range typeMapping {
if gType.Pkg == "" {
continue
}
found := false
// Check if specific type's package is imported already.
for _, importSpec := range file.Imports {
if importSpec.Path == nil {
continue
}
unquoted, err := strconv.Unquote(importSpec.Path.Value)
if err != nil {
return errors.Wrap(err, "could not unquote import path")
}
if gType.Pkg != "" && gType.Pkg == unquoted {
found = true
if importSpec.Name == nil {
// Assume the package name in the typemapping is correct.
continue
}
if importSpec.Name.Name == "." {
gType.PkgName = ""
} else {
gType.PkgName = importSpec.Name.Name
}
continue
}
}
if !found {
var nameIdent *ast.Ident
if gType.Aliased {
nameIdent = &ast.Ident{
Name: gType.PkgName,
}
}
outImports = append(outImports, &ast.ImportSpec{
Name: nameIdent,
Path: &ast.BasicLit{
Kind: token.STRING,
Value: strconv.Quote(gType.Pkg),
},
})
}
}
outImports = append(outImports, file.Imports...)
gctx.registerGenericTypes(file)
gctx.collectDependants(file)
/*
fmt.Println("Dependants")
for pos := range gctx.visited {
fmt.Println("\t", gctx.fset.Position(pos).String())
}
fmt.Printf("\n\n")
*/
if targetPackageName == "" {
targetPackageName = file.Name.Name
}
// rename types
gctx.doRenames(file)
// remove generic types from output
for p := range gctx.types {
if gctx.isGeneric[p] {
delete(gctx.types, p)
}
}
outFset := token.NewFileSet()
outFile := &ast.File{
Name: &ast.Ident{
Name: targetPackageName,
},
}
if len(outImports) > 0 {
importDecl := &ast.GenDecl{
Tok: token.IMPORT,
}
for _, spec := range outImports {
importDecl.Specs = append(importDecl.Specs, spec)
}
outFile.Decls = append(outFile.Decls, importDecl)
}
sortedTypes := sortSpecs(gctx.types)
for _, spec := range sortedTypes {
ts, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
newTs := &ast.TypeSpec{
Name: ts.Name,
Type: ts.Type,
}
decl := &ast.GenDecl{
Tok: token.TYPE,
Doc: gctx.renameComments(ts.Doc),
Specs: []ast.Spec{
newTs,
},
}
outFile.Decls = append(outFile.Decls, decl)
}
sortedConsts := sortSpecs(gctx.consts)
for _, spec := range sortedConsts {
vs, ok := spec.(*ast.ValueSpec)
if !ok {
continue
}
newVs := &ast.ValueSpec{
Names: vs.Names,
Type: vs.Type,
Values: vs.Values,
}
decl := &ast.GenDecl{
Tok: token.CONST,
Doc: gctx.renameComments(vs.Doc),
Specs: []ast.Spec{
newVs,
},
}
outFile.Decls = append(outFile.Decls, decl)
}
sortedVars := sortSpecs(gctx.vars)
for _, spec := range sortedVars {
vs, ok := spec.(*ast.ValueSpec)
if !ok {
continue
}
newVs := &ast.ValueSpec{
Names: vs.Names,
Type: vs.Type,
Values: vs.Values,
}
decl := &ast.GenDecl{
Tok: token.VAR,
Doc: gctx.renameComments(vs.Doc),
Specs: []ast.Spec{
newVs,
},
}
outFile.Decls = append(outFile.Decls, decl)
}
funcDecls := sortDecls(gctx.funcs)
for _, decl := range funcDecls {
fdecl, ok := decl.(*ast.FuncDecl)
if !ok {
continue
}
newFdecl := &ast.FuncDecl{
Doc: gctx.renameComments(fdecl.Doc),
Recv: fdecl.Recv,
Name: fdecl.Name,
Type: fdecl.Type,
Body: fdecl.Body,
}
outFile.Decls = append(outFile.Decls, newFdecl)
}
// newTokenPositioner().fixPositions(outFile)
clearPositions(outFile)
// ast.Print(outFset, outFile)
// TODO:
// If generating into different package than source, figure out dependencies,
// copy private stuff, reference public stuff.
buff := &bytes.Buffer{}
buff.WriteString("// Code generated by rei. DO NOT EDIT.\n\n")
err = printer.Fprint(buff, outFset, outFile)
if err != nil {
return errors.Wrap(err, "writing file failed")
}
outBytes, err := imports.Process(outFilename, buff.Bytes(), nil)
if err != nil {
return errors.Wrap(err, "Formatting file failed")
}
_, err = out.Write(outBytes)
return errors.Wrap(err, "writing file failed")
}