summaryrefslogtreecommitdiff
path: root/c++/tools/c_gen_iface.hpp
blob: 7e2e396ca87e3e94173a26f3f833f0e5a39813a1 (plain)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include <forstio/core/error.h>
#include <forstio/core/templates.h>
#include <forstio/codec/schema.h>

#include <string>
#include <sstream>
#include <map>

#include <iostream>

#include <forstio/codec/schema_stringify.h>

namespace saw {

namespace impl {
/**
 * Type meant to provide future help if I decide to introduce more maps
 */
struct c_types {
		struct c_member {
				std::string key;
				std::string name;
		};
		
		struct c_param {
				std::string key;
				std::string name;
				bool is_primitive;
		};

		struct c_struct {
				std::string def;
				std::string cpp_schema;
				std::vector<c_member> member_keys;
				std::string translate_to_c_source;
				std::string translate_to_cpp_source;
		};

		struct c_response {
				std::string key;
				bool is_primitive;
		};

		struct c_func {
				std::string def;
				std::string cpp_schema;
				c_response response;
				std::vector<c_param> requests;
				std::string source;
				std::string cpp_name;
		};

		using c_struct_map = std::map<std::string, c_struct>;
		using c_func_map = std::map<std::string, c_func>;

		struct state {
				std::string interface_schema;
				c_struct_map struct_map;
				c_func_map func_map;
				std::string prefix;
				std::string encoding;
		};
};

/**
 * 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 std::string_view value = "int8_t";
};

template<>
struct c_primitive_string<schema::Int16> {
	static constexpr std::string_view value = "int16_t";
};

template<>
struct c_primitive_string<schema::Int32> {
	static constexpr std::string_view value = "int32_t";
};

template<>
struct c_primitive_string<schema::Int64> {
	static constexpr std::string_view value = "int64_t";
};

template<>
struct c_primitive_string<schema::UInt8> {
	static constexpr std::string_view value = "uint8_t";
};

template<>
struct c_primitive_string<schema::UInt16> {
	static constexpr std::string_view value = "uint16_t";
};

template<>
struct c_primitive_string<schema::UInt32> {
	static constexpr std::string_view value = "uint32_t";
};

template<>
struct c_primitive_string<schema::UInt64> {
	static constexpr std::string_view value = "uint64_t";
};

template<>
struct c_primitive_string<schema::Float32> {
	static constexpr std::string_view value = "float";
};

template<>
struct c_primitive_string<schema::Float64> {
	static constexpr std::string_view value = "double";
};

template<typename Schema>
struct c_data_translater {
	static_assert(always_false<Schema>, "Not supported");
};

template<typename T, size_t N>
struct c_data_translater<schema::Primitive<T,N>> {
    using StructMap = typename c_types::c_struct_map;
	using Schema = schema::Primitive<T,N>;

	static error_or<void> generate(c_types::state& c_state, std::string& str){
		(void) c_state;
		str += c_primitive_string<Schema>::value;
		return void_t{};
	}
};

template<typename T, size_t Dim>
struct c_data_translater<schema::Array<T,Dim>> {
		using Schema = schema::Array<T,Dim>;
		using StructMap = typename c_types::c_struct_map;

	static error_or<void> generate(c_types::state& c_state, std::string& type_str){
		type_str = c_state.prefix + "_";
		type_str += "array_";

		std::string inner_type_str;
		auto eov = impl::c_data_translater<T>::generate(c_state, inner_type_str);
		if(eov.is_error()){
			return eov;
		}

		type_str += inner_type_str;

		if(Dim > 1){
			type_str += "_";
			type_str += std::to_string(Dim);
			type_str += "d";
		}

		if(c_state.struct_map.find(type_str) != c_state.struct_map.end()){
			return void_t{};
		}

		c_types::c_struct str;

		try {
				std::stringstream iss;
				schema_stringify<Schema>::apply(iss);
				str.cpp_schema = iss.str();
		}catch(const std::exception& e){
				return make_error<err::critical>();
		}

		{
				c_types::c_member memb;
				memb.key = inner_type_str;
				memb.name = "data";
				memb.is_primitive
				fn.members.emplace_back(std::move(memb));
		}

		str.def = "struct ";
		str.def += type_str;

		str.def += " {\n";

		str.def += "\t" + inner_type_str + "* data;\n";
		str.def += "\tsize_t size;\n";
		if( Dim > 1 ){
			str.def += "\tsize_t dims["+std::to_string(Dim)+"];\n";
		}

		str.def += "};\n";

		c_state.struct_map.emplace(std::make_pair(type_str, std::move(str)));
		return void_t{};
	}
};

template<typename Interface>
struct c_iface_translater {
	static_assert(always_false<Interface>,"Not supported");
};

template<class... Request, class Response, string_literal... Lits>
struct c_iface_translater<schema::Function<schema::Struct<schema::Member<Request, Lits>...>, Response>> {
    using StructMap = typename c_types::c_struct_map;
	using FuncMap = typename c_types::c_func_map;

	template<size_t i>
    static error_or<void> generate_request_member(c_types::state& c_state, std::array<std::string, sizeof...(Request)>& type_arr){
			using Type = typename parameter_pack_type<i, Request...>::type;
			// constexpr string_literal lit = parameter_key_pack_type<i, Lits...>::literal;

			auto eov = c_data_translater<Type>::generate(c_state, type_arr.at(i));
			if(eov.is_error()){
				return eov;
			}

			if constexpr ((i+1) < sizeof...(Request)){
				auto eov = generate_request_member<i+1>(c_state, type_arr);
				return eov;
			}

			return void_t{};
	}

	template<size_t i>
	static error_or<void> generate_parameter(std::string& param_use, const std::array<std::string, sizeof...(Request)>& req_type_arr){
		using Type = typename parameter_pack_type<i, Request...>::type;
		constexpr string_literal lit = parameter_key_pack_type<i, Lits...>::literal;
		
		std::string used_param;
		if constexpr (c_is_primitive<Type>::value){
				used_param += req_type_arr.at(i);
		} else {
				used_param += "const " + req_type_arr.at(i) + "*";
		}
		param_use += used_param + " " + std::string{lit.view()};

		if constexpr ( (i+1) < sizeof...(Request) ){
				param_use += ", ";
				return generate_parameter<i+1>(param_use, req_type_arr);
		}

		return void_t{};
	}

	static error_or<void> generate(c_types::state& c_state, const std::string& func_name){
		std::array<std::string, sizeof...(Request)> request_type_arr;

		if constexpr (sizeof...(Request) > 0){
				auto eov = generate_request_member<0>(c_state, request_type_arr);
				if(eov.is_error()){
						return eov;
				}
		}
		std::string response_type_str;
		{
				auto eov = c_data_translater<Response>::generate(c_state, response_type_str);
				if(eov.is_error()){
						return eov;
				}
		}
		std::string c_func_name = c_state.prefix + "_" + func_name;

		c_types::c_func fn;
		fn.cpp_name = func_name;
		fn.def = "int";
		std::string iface_ctx = c_state.prefix + "_iface_context* ctx";
		fn.def += " " + c_func_name + "(" + iface_ctx;
		fn.def += ", " + response_type_str + "* out";
		if constexpr ( sizeof...(Request) > 0 ){
				fn.def += ", ";
				auto eov = generate_parameter<0>(fn.def, request_type_arr);
				if(eov.is_error()){
						return eov;
				}
		}
		fn.def += ")";

		fn.source = "{\n";
		// Check necessary pointers
		fn.source += "\tassert(ctx);\n";
		fn.source += "\tif(!ctx){return -1;}\n\n";
		fn.source += "\tauto rmt = reinterpret_cast<interface<IfaceSchema, "
		fn.source += c_state.encoding;
		fn.source += ">*>(ctx);\n";

		// translate the input data
		if constexpr (sizeof...(Request) > 0){
				// I need a recursive search for the types
		}

		// Call the remote interface
		fn.source += "\tauto eov = rmt->template call<\"" + fn.cpp_name + "\">(std::move(input));";
		fn.source += ");\n";

		// translate the output data
		fn.source += "";

		// Close the brace

		fn.source += "}\n";
		
		c_state.func_map.emplace(std::make_pair(c_func_name, std::move(fn)));
		
		return void_t{};
	}
};

template<class... Requests, class... Responses, string_literal... Names>
struct c_iface_translater<schema::Interface<schema::Member<schema::Function<Requests, Responses>, Names>...>> {
	using Schema = schema::Interface<schema::Member<schema::Function<Requests, Responses>, Names>...>;

	template<size_t i>
	static error_or<void> generate_member(c_types::state& c_state){
		using Req = typename parameter_pack_type<i,Requests...>::type;
		using Resp = typename parameter_pack_type<i,Responses...>::type;
		constexpr string_literal lit = parameter_key_pack_type<i, Names...>::literal;

		try{
				std::stringstream iss;
				schema_stringify<Schema>::apply(iss);
				c_state.interface_schema = iss.str();
		}catch(const std::exception& e){
				(void) e;
				return make_error<err::critical>();
		}

		std::string c_func_name = c_state.prefix + "_" + std::string{lit.view()};
		if(c_state.func_map.find(c_func_name) != c_state.func_map.end()){
			return make_error<err::already_exists>();
		}

		{
			std::string type_str;
			auto eov = c_iface_translater<schema::Function<Req, Resp>>::generate(c_state, std::string{lit.view()});
			if(eov.is_error()){
				return eov;
			}
		}

		if constexpr ((i+1) < sizeof...(Requests) ){
			return generate_member<i+1>(c_state);
		}
		return void_t{};
	}

	static error_or<void> generate(c_types::state& c_state){
		if constexpr ( sizeof...(Requests) > 0 ){
			return generate_member<0>(c_state);
		}

		return void_t{};
	}
};
}

template<typename Interface>
error_or<void> generate_c_interface(typename impl::c_types::state& state){
	auto eov = impl::c_iface_translater<Interface>::generate(state);
	return eov;
}

error_or<void> generate_iface_example(){
    using Request1 = schema::Struct<
		schema::Member<schema::Int32, "foo">,
		schema::Member<schema::Float64, "bar">,
		schema::Member<schema::Array<schema::Float32,1>, "baz">
    >;
	using Response1 = schema::Int32;

	using Func1 = schema::Function<Request1, Response1>;

	using Iface = schema::Interface<
		schema::Member<Func1, "func_one">
    >;

	impl::c_types::state c_state;
	c_state.prefix = "c_saw";
	c_state.encoding = "saw::encode::Native";

	typename impl::c_types::c_struct_map& type_map = c_state.struct_map;
	typename impl::c_types::c_func_map& func_map = c_state.func_map;
	auto eov = generate_c_interface<Iface>(c_state);
    if(eov.is_error()){
		return eov;
	}
	std::cout<<"Interface: "<<c_state.interface_schema<<std::endl;
	std::cout<<"Prefix: "<<c_state.prefix<<std::endl;
	std::cout<<"\nTypes: "<<std::endl;

	for(auto& iter : type_map){
		std::cout<<"\nType: \""<<iter.first<<"\""<<std::endl;
		std::cout<<"Definition:\n\"\"\"\n";
		std::cout<<iter.second.def;
		std::cout<<"\"\"\""<<std::endl;
		std::cout<<"Schema: "<<iter.second.cpp_schema<<std::endl;
	}
	std::cout<<"\nFunctions: "<<std::endl;

	for(auto& iter : func_map){
		std::cout<<"\nSymbol: \""<<iter.first<<"\""<<std::endl;
		std::cout<<"Definition:\n\"\"\"\n";
		std::cout<<iter.second.def<<";\n";
		std::cout<<"\"\"\""<<std::endl;
		std::cout<<"\nSource:\n"<<iter.second.source<<std::endl;
	}

	return eov;
}

error_or<void> generate_array_example(){
	using Schema1 = schema::Array<schema::Int32,2>;
	
	using Schema2 = schema::Array<schema::Float64,5>;
	
	using Schema3 = schema::Array<schema::UInt16,1>;

	std::string prefix = "c_saw";
	impl::c_types::state c_state;
	c_state.prefix = prefix;
	{
		std::string type_str;
		auto eov = impl::c_data_translater<Schema1>::generate(c_state, type_str);
		if(eov.is_error()){
			return eov;
		}
	}
	{
		std::string type_str;
		auto eov = impl::c_data_translater<Schema2>::generate(c_state, type_str);
		if(eov.is_error()){
			return eov;
		}
	}
	{
		std::string type_str;
		auto eov = impl::c_data_translater<Schema3>::generate(c_state, type_str);
		if(eov.is_error()){
			return eov;
		}
	}

	std::cout<<"Prefix: "<<c_state.prefix<<std::endl;

	for(auto& iter : c_state.struct_map){
		std::cout<<"\nType: \""<<iter.first<<"\""<<std::endl;
		std::cout<<"Definition:\n\"\"\"\n";
		std::cout<<iter.second.def;
		std::cout<<"\"\"\""<<std::endl;
	}

	return void_t{};
}
}