-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtype-generator-number.go
175 lines (146 loc) · 4.09 KB
/
type-generator-number.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
package generate
import (
"fmt"
"github.com/pekim/jennifer/jen"
)
type TypeGeneratorNumber struct {
TypeGeneratorPanic
typ *Type
}
func TypeGeneratorIntegerNew(typ *Type) *TypeGeneratorNumber {
return &TypeGeneratorNumber{
typ: typ,
}
}
func (t *TypeGeneratorNumber) isSupportedAsParam(direction string) (supported bool, reason string) {
if t.typ.indirectLevel > 1 {
return false, fmt.Sprintf("%s with indirection level of %d",
t.typ.CType, t.typ.indirectLevel)
}
return true, ""
}
func (t *TypeGeneratorNumber) isSupportedAsArrayParam(direction string) (supported bool, reason string) {
return true, ""
}
func (t *TypeGeneratorNumber) isSupportedAsArrayParamC(direction string) (supported bool, reason string) {
return false, ""
}
func (t *TypeGeneratorNumber) isSupportedAsParamC() (supported bool, reason string) {
return true, ""
}
func (t *TypeGeneratorNumber) isSupportedAsField() (supported bool, reason string) {
if t.typ.indirectLevel > 0 {
return false, fmt.Sprintf("%s with indirection level of %d",
t.typ.CType, t.typ.indirectLevel)
}
return true, ""
}
func (t *TypeGeneratorNumber) isSupportedAsArrayReturnValue() (supported bool, reason string) {
return false, ""
}
func (t *TypeGeneratorNumber) isSupportedAsReturnValue() (supported bool, reason string) {
return true, ""
}
func (t *TypeGeneratorNumber) isSupportedAsReturnCValue() (supported bool, reason string) {
return true, ""
}
func (t *TypeGeneratorNumber) generateDeclaration(g *jen.Group, goVarName string) {
g.
Id(goVarName).
Do(t.typ.qname.generate)
}
func (t *TypeGeneratorNumber) generateArrayDeclaration(g *jen.Group, goVarName string) {
g.
Id(goVarName).
Index().
Do(t.typ.qname.generate)
}
func (t *TypeGeneratorNumber) generateArrayDeclarationC(g *jen.Group, cVarName string) {
}
func (t *TypeGeneratorNumber) generateDeclarationC(g *jen.Group, cVarName string) {
g.Id(cVarName).Qual("C", t.typ.CType)
}
func (t *TypeGeneratorNumber) generateParamCallArgument(g *jen.Group, cVarName string) {
g.
Do(func(s *jen.Statement) {
if t.typ.indirectLevel == 1 {
s.Op("&")
}
}).
Id(cVarName)
}
func (t *TypeGeneratorNumber) generateParamOutCallArgument(g *jen.Group, cVarName string) {
g.
Op("&").
Id(cVarName)
}
func (t *TypeGeneratorNumber) generateParamCVar(g *jen.Group, cVarName string, goVarName string, transferOwnership string) {
g.
Id(cVarName).
Op(":=").
Parens(jen.Qual("C", t.typ.cTypeName)).
Parens(jen.Id(goVarName))
}
func (t *TypeGeneratorNumber) generateParamGoVar(g *jen.Group, goVarName string, cVarName string, pkg string) {
g.
Id(goVarName).
Op(":=").
Id(numberCTypeMap[t.typ.CType]).
Call(jen.Id(cVarName))
}
func (t *TypeGeneratorNumber) generateParamOutCVar(g *jen.Group, cVarName string) {
g.
Var().
Id(cVarName).
Qual("C", t.typ.cTypeName)
}
func (t *TypeGeneratorNumber) generateReturnFunctionDeclaration(g *jen.Group) {
g.Do(t.typ.qname.generate)
}
func (t *TypeGeneratorNumber) generateReturnFunctionDeclarationCtype(g *jen.Group) {
g.Qual("C", t.typ.CType)
}
func (t *TypeGeneratorNumber) generateReturnCToGo(g *jen.Group, isParam bool,
cVarName string, goVarName string, pkg string, transferOwnership string, nullable bool) {
g.
Id(goVarName).
Op(":=").
Parens(jen.Do(t.typ.qname.generate)).
Parens(jen.Do(func(s *jen.Statement) {
if t.typ.Name == "gpointer" {
s.
Qual("unsafe", "Pointer").
CallFunc(func(g *jen.Group) {
if t.typ.indirectLevel == 1 {
g.
Op("&").
Id(cVarName)
} else {
g.Id(cVarName)
}
})
} else {
s.Id(cVarName)
}
}))
}
func (t *TypeGeneratorNumber) generateCToGo(pkg string, cVarReference *jen.Statement) *jen.Statement {
return jen.
Parens(jen.Do(func(s *jen.Statement) {
if t.typ.indirectLevel == 1 {
s.Op("*")
}
t.typ.qname.generate(s)
})).
Parens(jen.Do(func(s *jen.Statement) {
if t.typ.indirectLevel == 1 {
s.Op("&")
}
s.Add(cVarReference)
}))
}
func (t *TypeGeneratorNumber) generateGoToC(g *jen.Group, goVarReference *jen.Statement) {
g.
Parens(jen.Qual("C", t.typ.cTypeName)).
Parens(goVarReference)
}