forked from goplus/gop
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuiltingen.gox
132 lines (122 loc) · 3.04 KB
/
builtingen.gox
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
import (
"bytes"
"go/ast"
"go/format"
"go/token"
"gop/ast/gopq"
"gop/parser"
"os"
)
type builtinTI struct {
Methods []builtin
}
var (
Builtins []builtin
Types []*builtinTI
)
func gen() []byte {
f := &ast.File{
Name: ast.newIdent("builtin"),
Decls: make([]ast.Decl, 0, 128),
}
f.Decls <- &ast.GenDecl{
Tok: token.IMPORT,
Specs: []ast.Spec{
importSpec("gop/builtin/iox"),
importSpec("io"),
importSpec("os"),
importSpec("reflect"),
},
}
genDecls f
b := new(bytes.Buffer)
format.node! b, fset, f
return format.source(b.bytes)!
}
func genDecls(f *ast.File) {
for built <- Builtins {
f.Decls <- built.genAST()
}
for t <- Types {
mthds := t.Methods
for m <- mthds {
f.Decls <- m.genMethodAST(mthds)
}
}
}
func initBuiltinTIs(fn gopq.NodeSet) (tistr *builtinTI) {
ti := fn.body.any.assignStmt.rhs(0).x.compositeLit("BuiltinTI")
methods := ti.elt("methods").cache
for method <- methods {
aTI := &builtinTI{}
for item <- method.elt {
mthd := item.elt(0).unquotedString!
fn := item.elt(1).one
if ref := fn.callExpr.one; ref.ok {
pkg := ref.fun.x.ident!
name := ref.arg(0).unquotedString!
exargs := 0
if ex := item.elt(2).compositeLit("bmExargs").one; ex.ok {
exargs = ex.eltLen!
}
aTI.Methods <- builtin{mthd, {pkg, name, exargs}}
} else {
name := fn.ident!
aTI.Methods <- builtin{mthd, {"", name, 0}}
}
}
Types <- aTI
if len(aTI.Methods) > 10 {
tistr = aTI
}
}
return
}
func newBuiltinDefault(fn gopq.NodeSet, tistr *builtinTI) {
methods := fn.body.any.exprStmt.x.callExpr("ti.AddMethods").cache
for method <- methods {
aTI := &builtinTI{}
for arg <- method.varg(0).x {
mthd := arg.elt("Name").unquotedString!
ref := arg.elt("Fn").callExpr.one
pkg := ref.fun.x.ident!
name := ref.arg(0).unquotedString!
if pkg == "strx" {
tistr.Methods <- builtin{mthd, {pkg, name, 0}}
} else {
aTI.Methods <- builtin{mthd, {pkg, name, 0}}
}
}
if len(aTI.Methods) > 0 {
Types <- aTI
}
}
}
func initBuiltin(fn gopq.NodeSet) {
stmt := fn.body.any.exprStmt.x.cache
item := stmt.callExpr("scope.Insert").arg(0).cache
for call <- item.callExpr("gogen.NewOverloadFunc") {
built := call.arg(2).unquotedString!
if built != "newRange" { // hide builtin `newRange`
ref := call.arg(3).callExpr.one
pkg := ref.fun.x.ident!
name := ref.arg(0).unquotedString!
Builtins <- builtin{built, {pkg, name, 0}}
}
}
for call <- stmt.callExpr("initBuiltinFns") {
pkg := call.arg(2).ident!
builtins := call.arg(3).unquotedStringElts!
for built <- builtins {
Builtins <- builtin{built, {pkg, built.capitalize, 0}}
}
}
}
fns := gopq.fromFile(fset, "${root}/../gogen/builtin.go", nil, parser.ParseComments)!.funcs
tistr := initBuiltinTIs(fns.funcDecl("initBuiltinTIs").one)
fns = gopq.fromFile(fset, "${root}/cl/builtin.go", nil, parser.ParseComments)!.funcs
initBuiltin fns.funcDecl("initBuiltin").one
newBuiltinDefault fns.funcDecl("newBuiltinDefault").one, tistr
b := gen()
os.Stdout.write b
os.writeFile "${root}/builtin/doc/builtin.gop", b, 0777