-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgo_types.go
154 lines (136 loc) · 2.76 KB
/
go_types.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
package types
import (
"fmt"
"go/ast"
"go/token"
"strconv"
"strings"
)
type GoType = ast.Expr
func ident(name string) *ast.Ident {
return &ast.Ident{Name: name}
}
func goName(name string) string {
switch name {
case "len", "cap",
"var", "const", "func",
"type", "chan", "range",
"copy", "close", "make", "append",
"string", "byte", "rune",
"interface", "map":
name += "_"
}
name = strings.ReplaceAll(name, "$", "_")
if name == "_" {
name += "1"
}
return name
}
func (e *Ident) convertName() {
if e.GoName != "" {
return
}
e.GoName = goName(e.Name)
}
func (e *Ident) GoIdent() *ast.Ident {
e.convertName()
return ident(e.GoName)
}
func (t *unkType) GoType() GoType {
if t.isStruct {
return ident("struct{}") // TODO
}
return ident("interface{}") // TODO
}
func (t IntType) GoType() GoType {
u := "u"
if t.signed {
u = ""
}
return ident(fmt.Sprintf("%sint%d", u, t.Sizeof()*8))
}
func (t FloatType) GoType() GoType {
return ident(fmt.Sprintf("float%d", t.size*8))
}
func (t BoolType) GoType() GoType {
return ident("bool")
}
func (t *ptrType) GoType() GoType {
elem := t.Elem()
if elem == nil {
return ident("unsafe.Pointer")
}
return &ast.StarExpr{
X: elem.GoType(),
}
}
func (t namedPtr) GoType() GoType {
return t.name.GoIdent()
}
func (t ArrayType) GoType() GoType {
var sz ast.Expr
if !t.slice {
sz = &ast.BasicLit{
Kind: token.INT,
Value: strconv.Itoa(t.size),
}
}
return &ast.ArrayType{
Len: sz,
Elt: t.elem.GoType(),
}
}
func (t *namedType) GoType() GoType {
return t.name.GoIdent()
}
func (f *Field) GoField() *ast.Field {
var names []*ast.Ident
if f.Name != nil && !f.Name.IsUnnamed() {
names = append(names, f.Name.GoIdent())
}
return &ast.Field{Names: names, Type: f.Type().GoType()}
}
func (t *StructType) GoType() GoType {
fields := &ast.FieldList{}
if t.union {
fields.List = append(fields.List, &ast.Field{Type: ident("// union")})
}
for _, f := range t.fields {
fields.List = append(fields.List, f.GoField())
}
return &ast.StructType{Fields: fields}
}
func (t *FuncType) GoType() GoType {
return t.GoFuncType()
}
func (t *FuncType) GoFuncType() *ast.FuncType {
var ret ast.Expr
if t.Return() != nil {
ret = t.Return().GoType()
}
p := &ast.FieldList{}
f := &ast.FuncType{
Params: p,
}
if ret != nil {
f.Results = &ast.FieldList{List: []*ast.Field{{Type: ret}}}
}
hasNames := t.ArgN() == 0 // default to names, if only has variadic
for _, a := range t.Args() {
if a.Name.Name != "" {
hasNames = true
}
p.List = append(p.List, a.GoField())
}
if t.Variadic() {
var names []*ast.Ident
if hasNames {
names = []*ast.Ident{ident("_rest")}
}
p.List = append(p.List, &ast.Field{
Names: names,
Type: &ast.Ellipsis{Elt: ident("interface{}")},
})
}
return f
}