From 35635f5514a9f702b5606146bf9ff4494030ff8f Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Fri, 16 Feb 2024 15:50:40 +0100 Subject: core,tools,codec: Moving towards lang tooling --- modules/tools/c++/c_gen_iface.hpp | 155 +++++++++++++++++++++++++++---- modules/tools/python/c_generate_iface.py | 23 +++++ modules/tools/tests/c_iface.cpp | 20 ++++ 3 files changed, 182 insertions(+), 16 deletions(-) (limited to 'modules/tools') diff --git a/modules/tools/c++/c_gen_iface.hpp b/modules/tools/c++/c_gen_iface.hpp index 1b1e14d..ceb6fef 100644 --- a/modules/tools/c++/c_gen_iface.hpp +++ b/modules/tools/c++/c_gen_iface.hpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -16,19 +17,23 @@ namespace schema { using CVar = Struct< Member, + Member, Member, + Member, Member, Member >; using CStruct = Struct< Member, - Member, + Member, + Member, Member, "members"> >; using CFunction = Struct< Member, + Member, Member, Member, "params">, Member @@ -36,6 +41,7 @@ using CFunction = Struct< using CIface = Struct< Member, + Member, Member,"structs">, Member,"functions"> >; @@ -71,7 +77,8 @@ using FlattenedSchema = Struct< Member, Names>... >; } - */ +*/ + template struct schema_flattener { static_assert(always_false, "Not supported"); @@ -101,54 +108,84 @@ struct c_primitive_string { template<> struct c_primitive_string { - static constexpr std::string_view value = "int8_t"; + static constexpr string_literal name = "int8"; + static constexpr string_literal value = "int8_t"; }; template<> struct c_primitive_string { - static constexpr std::string_view value = "int16_t"; + static constexpr string_literal name = "int16"; + static constexpr string_literal value = "int16_t"; }; template<> struct c_primitive_string { - static constexpr std::string_view value = "int32_t"; + static constexpr string_literal name = "int32"; + static constexpr string_literal value = "int32_t"; }; template<> struct c_primitive_string { - static constexpr std::string_view value = "int64_t"; + static constexpr string_literal name = "int64"; + static constexpr string_literal value = "int64_t"; }; template<> struct c_primitive_string { - static constexpr std::string_view value = "uint8_t"; + static constexpr string_literal name = "uint8"; + static constexpr string_literal value = "uint8_t"; }; template<> struct c_primitive_string { - static constexpr std::string_view value = "uint16_t"; + static constexpr string_literal name = "uint16"; + static constexpr string_literal value = "uint16_t"; }; template<> struct c_primitive_string { - static constexpr std::string_view value = "uint32_t"; + static constexpr string_literal name = "uint32"; + static constexpr string_literal value = "uint32_t"; }; template<> struct c_primitive_string { - static constexpr std::string_view value = "uint64_t"; + static constexpr string_literal name = "uint64"; + static constexpr string_literal value = "uint64_t"; }; template<> struct c_primitive_string { - static constexpr std::string_view value = "float"; + static constexpr string_literal name = "float32"; + static constexpr string_literal value = "float"; }; template<> struct c_primitive_string { - static constexpr std::string_view value = "double"; + static constexpr string_literal name = "float64"; + static constexpr string_literal value = "double"; }; +error_or> c_interface_find_struct_by_crc32(const data& state, const data& id){ + const auto& strs = state.template get<"structs">(); + for(uint64_t i = 0; i < strs.size(); ++i){ + const auto& str = strs.at(i); + if(str.template get<"crc32">() == id){ + return data{i}; + } + } + return make_error(); +} + +error_or c_interface_add_struct(data& state, data&& val){ + auto& strs = state.template get<"structs">(); + auto eov = strs.add(std::move(val)); + if(eov.is_error()){ + return std::move(eov.get_error()); + } + return void_t{}; +} + bool c_interface_function_exists(data& state, const std::string_view& name){ auto& funcs = state.template get<"functions">(); for(uint64_t i = 0; i < funcs.size(); ++i){ @@ -167,14 +204,17 @@ struct c_data_translater { template struct c_data_translater> { using Schema = schema::Primitive; + static constexpr string_literal c_type_name = c_primitive_string::name; + static constexpr string_literal c_type = c_primitive_string::value; static error_or generate(data& state, data& prim){ try{ std::stringstream iss; schema_stringify::apply(iss); prim.template get<"schema">().set(iss.str()); + prim.template get<"crc32">().set(schema_hash::apply()); prim.template get<"is_primitive">().set(1); - prim.template get<"type">().set(std::string{c_primitive_string::value}); + prim.template get<"type">().set(std::string{c_primitive_string::value.view()}); }catch(const std::exception&){ return make_error(); } @@ -185,23 +225,104 @@ struct c_data_translater> { template struct c_data_translater> { + static_assert(N==1, "Doesn't support more than one dimension currently"); + using Schema = schema::Array; + static constexpr string_literal c_type_name = "array_"_sl + c_data_translater::c_type_name + "_1d"_sl; + static constexpr string_literal c_type = c_type_name + "_t"_sl; - static error_or generate(data& state){ + static error_or generate(data& state, data& arr){ + try{ + { + std::stringstream iss; + schema_stringify::apply(iss); + arr.template get<"schema">().set(iss.str()); + arr.template get<"crc32">().set(schema_hash::apply()); + arr.template get<"is_primitive">().set(0); + } + + /// @todo change this !!!!! + { + auto eov = c_interface_find_struct_by_crc32(state, arr.template get<"crc32">()); + if(eov.is_error()){ + if(false){ + return std::move(eov.get_error()); + } + data str; + str.template get<"schema">() = arr.template get<"schema">(); + str.template get<"crc32">() = arr.template get<"crc32">(); + str.template get<"type">().set(std::string{c_type.view()}); + auto& membs = str.template get<"members">(); + { + data c_var; + std::stringstream iss; + schema_stringify::apply(iss); + c_var.template get<"schema">().set(iss.str()); + c_var.template get<"crc32">().set(schema_hash::apply()); + if constexpr (is_primitive::value){ + c_var.template get<"type">().set(std::string{c_primitive_string::value.view()} + "*"); + c_var.template get<"name">().set(std::string{"data"}); + c_var.template get<"is_primitive">().set(1); + }else{ + return make_error(); + } + + auto eov_add = membs.add(std::move(c_var)); + if(eov_add.is_error()){ + return std::move(eov_add.get_error()); + } + } + { + data c_var; + std::stringstream iss; + schema_stringify::apply(iss); + c_var.template get<"schema">().set(iss.str()); + c_var.template get<"crc32">().set(schema_hash::apply()); + if constexpr (is_primitive::value){ + c_var.template get<"type">().set(std::string{c_primitive_string::value.view()}); + c_var.template get<"name">().set(std::string{"size"}); + c_var.template get<"is_primitive">().set(1); + }else{ + return make_error(); + } + + auto eov_add = membs.add(std::move(c_var)); + if(eov_add.is_error()){ + return std::move(eov_add.get_error()); + } + } + + /** + * Add the struct to the struct array + */ + { + auto eov_add = c_interface_add_struct(state, std::move(str)); + if(eov_add.is_error()){ + return std::move(eov_add.get_error()); + } + } + } + } + arr.template get<"type">().set(std::string{c_type.view()}); + }catch(const std::exception&){ + return make_error(); + } return void_t{}; } }; +/* template struct c_data_translater> { using Schema = schema::Struct; static error_or generate(data& state){ - return void_t{}; } }; +*/ + template struct c_parameter_translater { static_assert(always_false, "Type not supported"); @@ -218,7 +339,7 @@ struct c_parameter_translater...>> { auto c_var = data{}; c_var.template get<"name">().set(std::string{Literal.view()}); { - auto eov = c_data_translater::generate(state, c_var); + auto eov = c_data_translater::generate(state,c_var); if(eov.is_error()){ return eov; } @@ -266,6 +387,7 @@ struct c_data_translater> { std::stringstream iss; schema_stringify::apply(iss); function.template get<"schema">().set(iss.str()); + function.template get<"crc32">().set(schema_hash::apply()); }catch(const std::exception&){ return make_error(); } @@ -344,6 +466,7 @@ struct c_data_translater...>> { std::stringstream iss; schema_stringify::apply(iss); state.template get<"schema">().set(iss.str()); + state.template get<"crc32">().set(schema_hash::apply()); }catch(const std::exception&){ return make_error(); } diff --git a/modules/tools/python/c_generate_iface.py b/modules/tools/python/c_generate_iface.py index e5a0d9b..d234482 100755 --- a/modules/tools/python/c_generate_iface.py +++ b/modules/tools/python/c_generate_iface.py @@ -1 +1,24 @@ #!/usr/bin/env python3 + +import argparse; +import jinja2; + + +def parse_args(): + parser = argparse.ArgumentParser( + description = "Generates bindings for the Interface schema" + ); + parser.add_argument( + '-t', '--template', + required=True, + help = "Path to the template directory" + ); + + return parser.parse_args(); + +def main(): + return 0; + +if __name__ == "__main__": + rc = main(); + exit(rc); diff --git a/modules/tools/tests/c_iface.cpp b/modules/tools/tests/c_iface.cpp index 01f1106..4f1b744 100644 --- a/modules/tools/tests/c_iface.cpp +++ b/modules/tools/tests/c_iface.cpp @@ -14,6 +14,14 @@ using TestStruct = Struct< Member >; +using TestArray = Array< + Int64, 1 +>; + +using TestStructArray = Struct< + Member +>; + using TestEmptyInterface = Interface<>; using TestOneFunctionInterface = Interface< @@ -23,6 +31,10 @@ using TestOneFunctionInterface = Interface< using TestStructFunctionInterface = Interface< Member, "two"> >; + +using TestArrayFunctionInterface = Interface< + Member, "three"> +>; } template @@ -72,4 +84,12 @@ SAW_TEST("CIface Struct Function Interface"){ test_generate(res); std::cout<<"\n"<(res); + std::cout<<"\n"<