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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
#include <forstio/error.hpp>
#include <forstio/codec/data.hpp>
#include <forstio/codec/schema.hpp>
#include <forstio/codec/schema_hash.hpp>
#include <forstio/codec/schema_stringify.hpp>
#include <forstio/templates.hpp>
#include <string>
#include <sstream>
#include <map>
#include <iostream>
namespace saw {
namespace binding {
struct C {};
}
/**
* Helper to determine if we are dealing with primitive types
*/
template<typename Schema>
struct c_is_primitive {
static constexpr bool value = false;
};
template<typename T, size_t N>
struct c_is_primitive<schema::Primitive<T,N>> {
static constexpr bool value = true;
};
template<typename Schema>
struct c_primitive_string {
static_assert(always_false<Schema>, "Not supported");
};
template<>
struct c_primitive_string<schema::Int8> {
static constexpr string_literal name = "int8";
static constexpr string_literal value = "int8_t";
};
template<>
struct c_primitive_string<schema::Int16> {
static constexpr string_literal name = "int16";
static constexpr string_literal value = "int16_t";
};
template<>
struct c_primitive_string<schema::Int32> {
static constexpr string_literal name = "int32";
static constexpr string_literal value = "int32_t";
};
template<>
struct c_primitive_string<schema::Int64> {
static constexpr string_literal name = "int64";
static constexpr string_literal value = "int64_t";
};
template<>
struct c_primitive_string<schema::UInt8> {
static constexpr string_literal name = "uint8";
static constexpr string_literal value = "uint8_t";
};
template<>
struct c_primitive_string<schema::UInt16> {
static constexpr string_literal name = "uint16";
static constexpr string_literal value = "uint16_t";
};
template<>
struct c_primitive_string<schema::UInt32> {
static constexpr string_literal name = "uint32";
static constexpr string_literal value = "uint32_t";
};
template<>
struct c_primitive_string<schema::UInt64> {
static constexpr string_literal name = "uint64";
static constexpr string_literal value = "uint64_t";
};
template<>
struct c_primitive_string<schema::Float32> {
static constexpr string_literal name = "float32";
static constexpr string_literal value = "float";
};
template<>
struct c_primitive_string<schema::Float64> {
static constexpr string_literal name = "float64";
static constexpr string_literal value = "double";
};
struct language_binding_config {
std::string prefix;
};
namespace impl {
template<typename Schema, typename Lang>
struct lang_bind {
static_assert(always_false<Schema>, "Not supported");
};
struct language_binding_state {
struct info {
std::string type;
};
std::map<uint32_t, language_binding_state::info> hashes;
};
struct lang_bind_helper {
static error_or<void> append_string(buffer& buff, const std::string_view& str){
for(uint64_t i = 0; i < str.size(); ++i){
auto err = buff.push(static_cast<uint8_t>(str[i]));
if(!err.template is_type<err::no_error>()){
return err;
}
}
return void_t{};
}
static error_or<void> append_hashed_type(buffer& buff, const std::string_view& prefix, uint32_t hash){
{
auto eov = append_string(buff, prefix);
if(eov.is_error()){
return eov;
}
}
{
auto eov = append_string(buff, "_");
if(eov.is_error()){
return eov;
}
}
{
auto eov = append_string(buff, std::to_string(hash));
if(eov.is_error()){
return eov;
}
}
{
auto eov = append_string(buff, "_t");
if(eov.is_error()){
return eov;
}
}
return void_t{};
}
};
template<typename T, uint64_t L>
struct lang_bind<schema::Primitive<T,L>, binding::C> {
using Schema = schema::Primitive<T,L>;
static error_or<void> generate(buffer& buff, const language_binding_config& cfg, language_binding_state& state){
constexpr uint64_t hash = schema_hash<Schema>::apply();
{
std::string hash_type_str = cfg.prefix + "_" + std::to_string(hash) + "_t";
auto emp = state.hashes.emplace(std::make_pair(hash, hash_type_str));
if(emp.second){
{
auto eov = lang_bind_helper::append_string(buff, "typedef ");
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind_helper::append_string(buff, c_primitive_string<Schema>::value.view());
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind_helper::append_string(buff, " ");
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind_helper::append_string(buff, hash_type_str);
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind_helper::append_string(buff, ";\n");
if(eov.is_error()){
return eov;
}
}
}
}
return void_t{};
}
};
template<typename Input, typename Output>
struct lang_bind<schema::Function<Input, Output>, binding::C> {
static error_or<void> generate(buffer& buff, const language_binding_config& cfg, language_binding_state& state){
constexpr uint64_t input_hash = schema_hash<Input>::apply();
constexpr uint64_t output_hash = schema_hash<Output>::apply();
{
auto eov = lang_bind<Input, binding::C>::generate(buff, cfg, state);
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind<Output, binding::C>::generate(buff, cfg, state);
if(eov.is_error()){
return eov;
}
}
return void_t{};
}
};
template<typename... M>
struct lang_bind<schema::Interface<M...>, binding::C> {
template<uint64_t i>
static error_or<void> generate_element(buffer& buff, const language_binding_config& cfg, language_binding_state& state){
using Member = typename parameter_pack_type<i, M...>::type;
using MValue = typename Member::ValueType;
static constexpr string_literal MKey = Member::KeyLiteral;
{
auto eov = lang_bind<MValue, binding::C>::generate(buff, cfg, state);
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind_helper::append_string(buff, cfg.prefix);
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind_helper::append_string(buff, "_");
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind_helper::append_string(buff, MKey.view());
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind_helper::append_string(buff, "(");
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash<Member>::apply());
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind_helper::append_string(buff, ",");
if(eov.is_error()){
return eov;
}
}
{
auto eov = lang_bind_helper::append_string(buff, ");");
if(eov.is_error()){
return eov;
}
}
if constexpr ((i+1) < sizeof...(M) ){
return generate_element<i+1>(buff, cfg, state);
}
return void_t{};
}
static error_or<void> generate(buffer& buff, const language_binding_config& cfg){
if(cfg.prefix.size() == 0){
return make_error<err::invalid_state>("C interfaces need a prefix.");
}
language_binding_state state;
if constexpr (sizeof...(M) > 0){
return generate_element<0>(buff, cfg, state);
}
return void_t{};
}
};
}
template<typename IfaceSchema, typename Binding>
struct language_binding {
static_assert(always_false<IfaceSchema, Binding>, "Case not supported.");
};
template<typename IfaceSchema>
struct language_binding<IfaceSchema, binding::C> {
static error_or<void> generate(buffer& buff, const language_binding_config& cfg){
return impl::lang_bind<IfaceSchema, binding::C>::generate(buff, cfg);
}
};
}
|