diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-02-16 15:50:40 +0100 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-02-16 15:50:40 +0100 |
commit | 35635f5514a9f702b5606146bf9ff4494030ff8f (patch) | |
tree | e106742b502e93053943aaccd85040147a28c3de /modules/tools | |
parent | da93f0466cdeaf266debe5bacee6779354cf4a34 (diff) |
core,tools,codec: Moving towards lang tooling
Diffstat (limited to 'modules/tools')
-rw-r--r-- | modules/tools/c++/c_gen_iface.hpp | 155 | ||||
-rwxr-xr-x | modules/tools/python/c_generate_iface.py | 23 | ||||
-rw-r--r-- | modules/tools/tests/c_iface.cpp | 20 |
3 files changed, 182 insertions, 16 deletions
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 <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> @@ -16,19 +17,23 @@ namespace schema { using CVar = Struct< Member<schema::String, "schema">, + Member<schema::UInt32, "crc32">, Member<schema::String, "type">, + Member<schema::String, "type_name">, Member<schema::String, "name">, Member<schema::Int8, "is_primitive"> >; using CStruct = Struct< Member<schema::String, "schema">, - Member<schema::String, "name">, + Member<schema::UInt32, "crc32">, + Member<schema::String, "type">, Member<Array<CVar>, "members"> >; using CFunction = Struct< Member<schema::String, "schema">, + Member<schema::UInt32, "crc32">, Member<schema::String, "name">, Member<Array<CVar>, "params">, Member<CVar, "return"> @@ -36,6 +41,7 @@ using CFunction = Struct< using CIface = Struct< Member<schema::String, "schema">, + Member<schema::UInt32, "crc32">, Member<Array<CStruct>,"structs">, Member<Array<CFunction>,"functions"> >; @@ -71,7 +77,8 @@ using FlattenedSchema = Struct< Member<FlattenedSchemaElement<Ele>, Names>... >; } - */ +*/ + template<typename T, typename Res> struct schema_flattener { static_assert(always_false<T>, "Not supported"); @@ -101,54 +108,84 @@ struct c_primitive_string { template<> struct c_primitive_string<schema::Int8> { - 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<schema::Int16> { - 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<schema::Int32> { - 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<schema::Int64> { - 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<schema::UInt8> { - 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<schema::UInt16> { - 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<schema::UInt32> { - 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<schema::UInt64> { - 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<schema::Float32> { - static constexpr std::string_view value = "float"; + static constexpr string_literal name = "float32"; + static constexpr string_literal value = "float"; }; template<> struct c_primitive_string<schema::Float64> { - static constexpr std::string_view value = "double"; + static constexpr string_literal name = "float64"; + static constexpr string_literal value = "double"; }; +error_or<data<schema::UInt64>> c_interface_find_struct_by_crc32(const data<schema::CIface>& state, const data<schema::UInt32>& 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<schema::UInt64>{i}; + } + } + return make_error<err::not_found>(); +} + +error_or<void> c_interface_add_struct(data<schema::CIface>& state, data<schema::CStruct>&& 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<schema::CIface>& 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<typename T, uint64_t N> struct c_data_translater<schema::Primitive<T,N>> { using Schema = schema::Primitive<T,N>; + static constexpr string_literal c_type_name = c_primitive_string<Schema>::name; + static constexpr string_literal c_type = c_primitive_string<Schema>::value; static error_or<void> generate(data<schema::CIface>& state, data<schema::CVar>& prim){ try{ std::stringstream iss; schema_stringify<Schema>::apply(iss); prim.template get<"schema">().set(iss.str()); + prim.template get<"crc32">().set(schema_hash<Schema>::apply()); prim.template get<"is_primitive">().set(1); - prim.template get<"type">().set(std::string{c_primitive_string<Schema>::value}); + prim.template get<"type">().set(std::string{c_primitive_string<Schema>::value.view()}); }catch(const std::exception&){ return make_error<err::out_of_memory>(); } @@ -185,23 +225,104 @@ struct c_data_translater<schema::Primitive<T,N>> { template<typename T, uint64_t N> struct c_data_translater<schema::Array<T,N>> { + static_assert(N==1, "Doesn't support more than one dimension currently"); + using Schema = schema::Array<T,N>; + static constexpr string_literal c_type_name = "array_"_sl + c_data_translater<T>::c_type_name + "_1d"_sl; + static constexpr string_literal c_type = c_type_name + "_t"_sl; - static error_or<void> generate(data<schema::CIface>& state){ + static error_or<void> generate(data<schema::CIface>& state, data<schema::CVar>& arr){ + try{ + { + std::stringstream iss; + schema_stringify<Schema>::apply(iss); + arr.template get<"schema">().set(iss.str()); + arr.template get<"crc32">().set(schema_hash<Schema>::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<schema::CStruct> 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<schema::CVar> c_var; + std::stringstream iss; + schema_stringify<T>::apply(iss); + c_var.template get<"schema">().set(iss.str()); + c_var.template get<"crc32">().set(schema_hash<T>::apply()); + if constexpr (is_primitive<T>::value){ + c_var.template get<"type">().set(std::string{c_primitive_string<T>::value.view()} + "*"); + c_var.template get<"name">().set(std::string{"data"}); + c_var.template get<"is_primitive">().set(1); + }else{ + return make_error<err::not_implemented>(); + } + + auto eov_add = membs.add(std::move(c_var)); + if(eov_add.is_error()){ + return std::move(eov_add.get_error()); + } + } + { + data<schema::CVar> c_var; + std::stringstream iss; + schema_stringify<schema::UInt64>::apply(iss); + c_var.template get<"schema">().set(iss.str()); + c_var.template get<"crc32">().set(schema_hash<schema::UInt64>::apply()); + if constexpr (is_primitive<T>::value){ + c_var.template get<"type">().set(std::string{c_primitive_string<schema::UInt64>::value.view()}); + c_var.template get<"name">().set(std::string{"size"}); + c_var.template get<"is_primitive">().set(1); + }else{ + return make_error<err::not_implemented>(); + } + + 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<err::out_of_memory>(); + } return void_t{}; } }; +/* template<typename... Members> struct c_data_translater<schema::Struct<Members...>> { using Schema = schema::Struct<Members...>; static error_or<void> generate(data<schema::CIface>& state){ - return void_t{}; } }; +*/ + template<typename Str> struct c_parameter_translater { static_assert(always_false<Str>, "Type not supported"); @@ -218,7 +339,7 @@ struct c_parameter_translater<schema::Struct<schema::Member<V,K>...>> { auto c_var = data<schema::CVar>{}; c_var.template get<"name">().set(std::string{Literal.view()}); { - auto eov = c_data_translater<Type>::generate(state, c_var); + auto eov = c_data_translater<Type>::generate(state,c_var); if(eov.is_error()){ return eov; } @@ -266,6 +387,7 @@ struct c_data_translater<schema::Function<Req,Ret>> { std::stringstream iss; schema_stringify<Schema>::apply(iss); function.template get<"schema">().set(iss.str()); + function.template get<"crc32">().set(schema_hash<Schema>::apply()); }catch(const std::exception&){ return make_error<err::out_of_memory>(); } @@ -344,6 +466,7 @@ struct c_data_translater<schema::Interface<schema::Member<Funcs,Names>...>> { std::stringstream iss; schema_stringify<Schema>::apply(iss); state.template get<"schema">().set(iss.str()); + state.template get<"crc32">().set(schema_hash<Schema>::apply()); }catch(const std::exception&){ return make_error<err::out_of_memory>(); } 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<Float64, "b"> >; +using TestArray = Array< + Int64, 1 +>; + +using TestStructArray = Struct< + Member<TestArray, "array"> +>; + using TestEmptyInterface = Interface<>; using TestOneFunctionInterface = Interface< @@ -23,6 +31,10 @@ using TestOneFunctionInterface = Interface< using TestStructFunctionInterface = Interface< Member<Function<TestStruct, Int32>, "two"> >; + +using TestArrayFunctionInterface = Interface< + Member<Function<TestStructArray, Int32>, "three"> +>; } template<typename Schema> @@ -72,4 +84,12 @@ SAW_TEST("CIface Struct Function Interface"){ test_generate<schema::TestStructFunctionInterface>(res); std::cout<<"\n"<<res<<"\n"<<std::endl; } + +SAW_TEST("CIface Array Function Interface"){ + using namespace saw; + + std::string res; + test_generate<schema::TestArrayFunctionInterface>(res); + std::cout<<"\n"<<res<<"\n"<<std::endl; +} } |