-
Notifications
You must be signed in to change notification settings - Fork 5
/
astutil.go
192 lines (161 loc) · 2.8 KB
/
astutil.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
package gen
import (
"fmt"
"go/ast"
"go/token"
)
func accessMember(variable string, member string) *ast.SelectorExpr {
return &ast.SelectorExpr{
X: &ast.Ident{
Name: variable,
},
Sel: &ast.Ident{
Name: member,
},
}
}
func doCast(typ string, args ...ast.Expr) *ast.CallExpr {
return &ast.CallExpr{
Fun: &ast.Ident{
Name: typ,
},
Args: args,
}
}
func doCompose(typ string, v ast.Expr) *ast.CompositeLit {
return &ast.CompositeLit{
Type: &ast.Ident{
Name: typ,
},
Elts: []ast.Expr{
v,
},
}
}
func doCType(c string) *ast.SelectorExpr {
return accessMember("C", c)
}
func doCCast(typ string, args ...ast.Expr) *ast.CallExpr {
return doCall("C", typ, args...)
}
func doDeclare(name string, typ ast.Expr) *ast.DeclStmt {
return &ast.DeclStmt{
Decl: &ast.GenDecl{
Tok: token.VAR,
Specs: []ast.Spec{
&ast.ValueSpec{
Names: []*ast.Ident{
{
Name: name,
},
},
Type: typ,
},
},
},
}
}
func doField(name string, typ Type) *ast.Field {
f := &ast.Field{}
if name != "" {
f.Names = []*ast.Ident{
{
Name: name,
},
}
}
if typ.GoName != "" {
f.Type = doGoType(typ)
}
return f
}
func doGoType(typ Type) ast.Expr {
var r ast.Expr
if typ.PointerLevel > 0 && typ.CGoName == CSChar {
r = &ast.Ident{
Name: "string",
}
} else {
r = &ast.Ident{
Name: typ.GoName,
}
}
pl := typ.PointerLevel
if typ.IsSlice {
pl--
}
if pl > 0 && typ.CGoName != CSChar && !typ.IsReturnArgument {
for pl != 0 {
r = &ast.StarExpr{
X: r,
}
pl--
}
}
if typ.IsSlice {
r = &ast.ArrayType{
Elt: r,
}
}
return r
}
func doCall(variable string, method string, args ...ast.Expr) *ast.CallExpr {
return &ast.CallExpr{
Fun: accessMember(variable, method),
Args: args,
}
}
func doReference(x ast.Expr) *ast.UnaryExpr {
return &ast.UnaryExpr{
Op: token.AND,
X: x,
}
}
func doPointer(x ast.Expr) *ast.StarExpr {
return &ast.StarExpr{
X: x,
}
}
func doUnreference(x ast.Expr) *ast.StarExpr {
return &ast.StarExpr{
X: x,
}
}
func doZero() *ast.BasicLit {
return &ast.BasicLit{
Kind: token.INT,
Value: "0",
}
}
func doSwitchStmt(tag ast.Expr) *ast.SwitchStmt {
return &ast.SwitchStmt{
Tag: tag,
Body: &ast.BlockStmt{},
}
}
func doCaseClause(clause []ast.Expr, body []ast.Stmt) *ast.CaseClause {
return &ast.CaseClause{
List: clause,
Body: body,
}
}
func doStringLit(value string) *ast.BasicLit {
return &ast.BasicLit{
Kind: token.STRING,
Value: fmt.Sprintf("%q", value),
}
}
func sliceType(typ Type) ast.Expr {
var sliceType ast.Expr
if typ.PointerLevel > 0 && typ.CGoName == CSChar {
sliceType = doCType(CChar)
} else {
sliceType = doCType(typ.CGoName)
}
for i := 1; i < typ.PointerLevel; i++ {
sliceType = &ast.StarExpr{
X: sliceType,
}
}
return sliceType
}