forked from xlab/c-for-go
/
gen_const.go
179 lines (165 loc) · 4.72 KB
/
gen_const.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
package generator
import (
"bytes"
"fmt"
"io"
tl "github.com/xlab/cgogen/translator"
)
func (gen *Generator) writeDefinesGroup(wr io.Writer, defines []*tl.CDecl) (n int) {
writeStartConst(wr)
for _, decl := range defines {
if !decl.IsDefine {
continue
}
name := gen.tr.TransformName(tl.TargetConst, decl.Name)
fmt.Fprintf(wr, "// %s as defined in %s\n", name,
gen.tr.SrcLocation(tl.TargetConst, decl.Name, decl.Pos))
if decl.Value != nil {
fmt.Fprintf(wr, "%s = %v", name, decl.Value)
} else {
fmt.Fprintf(wr, "%s = %s", name, decl.Expression)
}
writeSpace(wr, 1)
n++
}
writeEndConst(wr)
return
}
func (gen *Generator) writeConstDeclaration(wr io.Writer, decl *tl.CDecl) {
declName := gen.tr.TransformName(tl.TargetConst, decl.Name)
fmt.Fprintf(wr, "// %s as declared in %s\n", declName,
gen.tr.SrcLocation(tl.TargetConst, decl.Name, decl.Pos))
goSpec := gen.tr.TranslateSpec(decl.Spec)
if decl.Value != nil {
fmt.Fprintf(wr, "const %s %s = %v", declName, goSpec, decl.Value)
return
}
fmt.Fprintf(wr, "const %s %s = %s", declName, goSpec, decl.Expression)
}
func (gen *Generator) expandEnumAnonymous(wr io.Writer, decl *tl.CDecl, namesSeen map[string]bool) {
var typeName []byte
var hasType bool
if decl.IsTypedef {
if typeName = gen.tr.TransformName(tl.TargetType, decl.Name); len(typeName) > 0 {
hasType = true
}
}
spec := decl.Spec.(*tl.CEnumSpec)
if hasType {
enumType := gen.tr.TranslateSpec(&spec.Type)
fmt.Fprintf(wr, "// %s as declared in %s\n", typeName,
gen.tr.SrcLocation(tl.TargetConst, decl.Name, decl.Pos))
fmt.Fprintf(wr, "type %s %s\n", typeName, enumType)
writeSpace(wr, 1)
fmt.Fprintf(wr, "// %s enumeration from %s\n", typeName,
gen.tr.SrcLocation(tl.TargetConst, decl.Name, decl.Pos))
}
writeStartConst(wr)
for i, m := range spec.Members {
if !gen.tr.IsAcceptableName(tl.TargetConst, m.Name) {
continue
}
mName := gen.tr.TransformName(tl.TargetConst, m.Name)
if len(mName) == 0 {
continue
} else if namesSeen[string(mName)] {
continue
} else {
namesSeen[string(mName)] = true
}
if !hasType {
fmt.Fprintf(wr, "// %s as declared in %s\n", mName,
gen.tr.SrcLocation(tl.TargetConst, m.Name, m.Pos))
}
switch {
case m.Value != nil:
if hasType {
fmt.Fprintf(wr, "%s %s = %s\n", mName, typeName, iotaOnZero(i, m.Value))
continue
}
fmt.Fprintf(wr, "%s = %s\n", mName, iotaOnZero(i, m.Value))
case len(m.Expression) != 0:
if hasType {
fmt.Fprintf(wr, "%s %s = %s\n", mName, typeName, iotaOnZero(i, m.Expression))
continue
}
fmt.Fprintf(wr, "%s = %s\n", mName, iotaOnZero(i, m.Expression))
default:
if i == 0 && hasType {
fmt.Fprintf(wr, "%s %s = iota\n", mName, typeName)
continue
} else if i == 0 {
fmt.Fprintf(wr, "%s = iota\n", mName)
}
fmt.Fprintf(wr, "%s\n", mName)
}
}
writeEndConst(wr)
writeSpace(wr, 1)
}
func (gen *Generator) expandEnum(wr io.Writer, decl *tl.CDecl) {
var declName []byte
var isTypedef bool
if decl.IsTypedef {
if declName = gen.tr.TransformName(tl.TargetType, decl.Name); len(declName) > 0 {
isTypedef = true
}
}
spec := decl.Spec.(*tl.CEnumSpec)
tagName := gen.tr.TransformName(tl.TargetType, decl.Spec.GetBase())
enumType := gen.tr.TranslateSpec(&spec.Type)
fmt.Fprintf(wr, "// %s as declared in %s\n", tagName,
gen.tr.SrcLocation(tl.TargetConst, decl.Name, decl.Pos))
fmt.Fprintf(wr, "type %s %s\n", tagName, enumType)
writeSpace(wr, 1)
if isTypedef {
if !bytes.Equal(tagName, declName) && len(declName) > 0 {
// alias type decl name to the tag
fmt.Fprintf(wr, "// %s as declared in %s\n", declName,
gen.tr.SrcLocation(tl.TargetConst, decl.Name, decl.Pos))
fmt.Fprintf(wr, "type %s %s", declName, tagName)
writeSpace(wr, 1)
}
}
fmt.Fprintf(wr, "// %s enumeration from %s\n", tagName,
gen.tr.SrcLocation(tl.TargetConst, decl.Name, decl.Pos))
writeStartConst(wr)
for i, m := range spec.Members {
if !gen.tr.IsAcceptableName(tl.TargetConst, m.Name) {
continue
}
mName := gen.tr.TransformName(tl.TargetConst, m.Name)
if len(mName) == 0 {
continue
}
switch {
case m.Value != nil:
fmt.Fprintf(wr, "%s %s = %v\n", mName, declName, iotaOnZero(i, m.Value))
case len(m.Expression) != 0:
fmt.Fprintf(wr, "%s %s = %v\n", mName, declName, iotaOnZero(i, m.Expression))
default:
if i == 0 {
fmt.Fprintf(wr, "%s %s = iota\n", mName, declName)
continue
}
fmt.Fprintf(wr, "%s\n", mName)
}
}
writeEndConst(wr)
writeSpace(wr, 1)
}
func iotaOnZero(i int, v interface{}) string {
result := fmt.Sprintf("%v", v)
if i == 0 {
if result == "0" {
return "iota"
}
}
return result
}
func writeStartConst(wr io.Writer) {
fmt.Fprintln(wr, "const (")
}
func writeEndConst(wr io.Writer) {
fmt.Fprintln(wr, ")")
}