/
cxx.go
243 lines (216 loc) · 6.07 KB
/
cxx.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright ©2017 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rmeta
import (
"fmt"
"reflect"
"strings"
"go-hep.org/x/hep/groot/root"
)
var GoType2ROOTEnum = map[reflect.Type]Enum{
reflect.TypeOf(int8(0)): Char,
reflect.TypeOf(int16(0)): Short,
reflect.TypeOf(int32(0)): Int,
reflect.TypeOf(int64(0)): Long,
// reflect.TypeOf(int64(0)): Long64,
reflect.TypeOf(float32(0)): Float,
reflect.TypeOf(float64(0)): Double,
reflect.TypeOf(uint8(0)): UChar,
// reflect.TypeOf(uint8(0)): CharStar,
reflect.TypeOf(uint16(0)): UShort,
reflect.TypeOf(uint32(0)): UInt,
reflect.TypeOf(uint64(0)): ULong,
// reflect.TypeOf(uint64(0)): ULong64,
reflect.TypeOf(false): Bool,
reflect.TypeOf(root.Float16(0)): Float16,
reflect.TypeOf(root.Double32(0)): Double32,
reflect.TypeOf(""): TString,
}
var GoType2Cxx = map[string]string{
"bool": "bool",
"byte": "unsigned char",
"uint": "unsigned int",
"uint8": "unsigned char",
"uint16": "unsigned short",
"uint32": "unsigned int",
"uint64": "unsigned long",
"int": "int",
"int8": "char",
"int16": "short",
"int32": "int",
"int64": "long",
"float32": "float",
"float64": "double",
}
var CxxBuiltins = map[string]reflect.Type{
"bool": reflect.TypeOf(false),
/*
"uint": reflect.TypeOf(uint(0)),
"uint8": reflect.TypeOf(uint8(0)),
"uint16": reflect.TypeOf(uint16(0)),
"uint32": reflect.TypeOf(uint32(0)),
"uint64": reflect.TypeOf(uint64(0)),
"int": reflect.TypeOf(int(0)),
"int8": reflect.TypeOf(int8(0)),
"int16": reflect.TypeOf(int16(0)),
"int32": reflect.TypeOf(int32(0)),
"int64": reflect.TypeOf(int64(0)),
"float32": reflect.TypeOf(float32(0)),
"float64": reflect.TypeOf(float64(0)),
*/
// stdint
"int8_t": reflect.TypeOf(int8(0)),
"int16_t": reflect.TypeOf(int16(0)),
"int32_t": reflect.TypeOf(int32(0)),
"int64_t": reflect.TypeOf(int64(0)),
"uint8_t": reflect.TypeOf(uint8(0)),
"uint16_t": reflect.TypeOf(uint16(0)),
"uint32_t": reflect.TypeOf(uint32(0)),
"uint64_t": reflect.TypeOf(uint64(0)),
// C/C++ builtins
"unsigned": reflect.TypeOf(uint32(0)),
"unsigned char": reflect.TypeOf(uint8(0)),
"unsigned short": reflect.TypeOf(uint16(0)),
"unsigned int": reflect.TypeOf(uint32(0)),
"unsigned long": reflect.TypeOf(uint64(0)),
//"int": reflect.TypeOf(int(0)),
"char": reflect.TypeOf(int8(0)),
"short": reflect.TypeOf(int16(0)),
"int": reflect.TypeOf(int32(0)),
"long": reflect.TypeOf(int64(0)),
"float": reflect.TypeOf(float32(0)),
"double": reflect.TypeOf(float64(0)),
"string": reflect.TypeOf(""),
// ROOT builtins
"Bool_t": reflect.TypeOf(true),
"Byte_t": reflect.TypeOf(uint8(0)),
"Char_t": reflect.TypeOf(int8(0)),
"UChar_t": reflect.TypeOf(uint8(0)),
"Short_t": reflect.TypeOf(int16(0)),
"UShort_t": reflect.TypeOf(uint16(0)),
"Int_t": reflect.TypeOf(int32(0)),
"UInt_t": reflect.TypeOf(uint32(0)),
"Seek_t": reflect.TypeOf(int64(0)), // FIXME(sbinet): not portable
"Long_t": reflect.TypeOf(int64(0)), // FIXME(sbinet): not portable
"ULong_t": reflect.TypeOf(uint64(0)), // FIXME(sbinet): not portable
"Long64_t": reflect.TypeOf(int64(0)),
"ULong64_t": reflect.TypeOf(uint64(0)),
"Float_t": reflect.TypeOf(float32(0)),
"Float16_t": reflect.TypeOf(root.Float16(0)),
"Double_t": reflect.TypeOf(float64(0)),
"Double32_t": reflect.TypeOf(root.Double32(0)),
"Version_t": reflect.TypeOf(int16(0)),
"Option_t": reflect.TypeOf(""),
"Ssiz_t": reflect.TypeOf(int(0)),
"Real_t": reflect.TypeOf(float32(0)),
"Axis_t": reflect.TypeOf(float64(0)),
"Stat_t": reflect.TypeOf(float64(0)),
"Font_t": reflect.TypeOf(int16(0)),
"Style_t": reflect.TypeOf(int16(0)),
"Marker_t": reflect.TypeOf(int16(0)),
"Width_t": reflect.TypeOf(int16(0)),
"Color_t": reflect.TypeOf(int16(0)),
"SCoord_t": reflect.TypeOf(int16(0)),
"Coord_t": reflect.TypeOf(float64(0)),
"Angle_t": reflect.TypeOf(float32(0)),
"Size_t": reflect.TypeOf(float32(0)),
}
func STLNameFrom(name string, vtype ESTLType, ctype Enum) string {
if ctype == Object {
return name
}
return STLNameFor(vtype, ctype)
}
// STLNameFor creates a regular C++ STL container name given a STL enum type
// and a ROOT enum value for the contained element.
func STLNameFor(vtype ESTLType, ctype Enum) string {
ename := rmeta2Name(ctype)
if strings.HasSuffix(ename, ">") {
ename += " "
}
typfmt := "%s"
switch vtype {
case STLvector:
typfmt = "vector<%s>"
case STLlist:
typfmt = "list<%s>"
case STLdeque:
typfmt = "deque<%s>"
case STLmap:
typfmt = "map<%s>"
case STLmultimap:
typfmt = "multimap<%s>"
case STLset:
typfmt = "set<%s>"
case STLmultiset:
typfmt = "multiset<%s>"
case STLbitset:
typfmt = "bitset<%s>"
case STLforwardlist:
typfmt = "forward_list<%s>"
case STLunorderedset:
typfmt = "unordered_set<%s>"
case STLunorderedmultiset:
typfmt = "unordered_multiset<%s>"
case STLunorderedmap:
typfmt = "unordered_map<%s>"
case STLunorderedmultimap:
typfmt = "unordered_multimap<%s>"
}
return fmt.Sprintf(typfmt, ename)
}
func rmeta2Name(t Enum) string {
switch t {
case Char:
return "char"
case Short:
return "short"
case Int:
return "int"
case Long:
return "long"
case Float:
return "float"
case Counter:
return "int"
case CharStar:
return "char*"
case Double:
return "double"
case Double32:
return "Double32_t"
case LegacyChar:
return "char" // FIXME(sbinet)
case UChar:
return "unsigned char"
case UShort:
return "unsigned short"
case UInt:
return "unsigned int"
case ULong:
return "unsigned long"
case Bits:
return "bits" // FIXME(sbinet)
case Long64:
return "Long64_t"
case ULong64:
return "ULong64_t"
case Bool:
return "bool"
case Float16:
return "Float16_t"
case Object:
// class derived from TObject
return "TObject" // FIXME(sbinet): better handling?
case TString:
return "TString"
case TObject:
return "TObject"
case TNamed:
return "TNamed"
case STLstring:
return "string"
}
panic(fmt.Errorf("not implemented: t=%v (%d)", t, t))
}