diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-03-10 20:24:11 +0100 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2024-03-10 20:24:11 +0100 |
commit | ff342be6178fd0e4a9b7a5f9653ac5d8dc979d49 (patch) | |
tree | 10b03aa90aea90ed3feab350d231ea84a76785d1 /modules/tools | |
parent | f2c255560d4ac83bdcb87c2d1a75a95ec540508c (diff) |
tools: Replace parameter type names in functions with their alias
Diffstat (limited to 'modules/tools')
-rw-r--r-- | modules/tools/c++/c_gen_iface.hpp | 115 | ||||
-rw-r--r-- | modules/tools/tests/c_iface.cpp | 18 |
2 files changed, 89 insertions, 44 deletions
diff --git a/modules/tools/c++/c_gen_iface.hpp b/modules/tools/c++/c_gen_iface.hpp index 7c26f80..d671c65 100644 --- a/modules/tools/c++/c_gen_iface.hpp +++ b/modules/tools/c++/c_gen_iface.hpp @@ -156,7 +156,7 @@ 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){ + static error_or<void> generate(buffer& buff, buffer& src, const language_binding_config& cfg, language_binding_state& state){ constexpr uint64_t hash = schema_hash<Schema>::apply(); { @@ -206,20 +206,20 @@ struct lang_bind<schema::Array<T,D>, binding::C> { static_assert(is_primitive<T>::value, "Currently only primitive type arrays are supported"); - static error_or<void> generate(buffer& buff, const language_binding_config& cfg, language_binding_state& state){ + static error_or<void> generate(buffer& buff, buffer& src, 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<schema::UInt64, binding::C>::generate(buff, cfg, state); + auto eov = lang_bind<schema::UInt64, binding::C>::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind<T, binding::C>::generate(buff, cfg, state); + auto eov = lang_bind<T, binding::C>::generate(buff, src, cfg, state); if(eov.is_error()){ return eov; } @@ -275,38 +275,33 @@ struct lang_bind<schema::Array<T,D>, binding::C> { template<typename Input, typename Output> struct lang_bind<schema::Function<Input, Output>, binding::C> { - static error_or<void> append_function_def(buffer& buff, - - static error_or<void> generate(buffer& buff, buffer& src, const language_binding_config& cfg, language_binding_state& state, const std::string_view& f_name){ - constexpr uint64_t input_hash = schema_hash<Input>::apply(); - constexpr uint64_t output_hash = schema_hash<Output>::apply(); - + static error_or<void> append_function_def(buffer& buff, const language_binding_config& cfg, language_binding_state& state, const std::string_view& f_name){ { - auto eov = lang_bind<Input, binding::C>::generate(buff, cfg, state); - if(eov.is_error()){ - return eov; - } + auto eov = lang_bind_helper::append_string(buff, "int "); + if(eov.is_error()){ + return eov; + } } { - auto eov = lang_bind<Output, 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, "typedef "); + 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<Input>::apply()); + auto eov = lang_bind_helper::append_string(buff, f_name); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, " "); + auto eov = lang_bind_helper::append_string(buff, " ( const "); if(eov.is_error()){ return eov; } @@ -330,11 +325,55 @@ struct lang_bind<schema::Function<Input, Output>, binding::C> { } } { - auto eov = lang_bind_helper::append_string(buff, "_input_t;\n"); + auto eov = lang_bind_helper::append_string(buff, "_input_t* input, "); + 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, f_name); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind_helper::append_string(buff, "_output_t* output )"); if(eov.is_error()){ return eov; } } + + return void_t{}; + } + + static error_or<void> generate(buffer& buff, buffer& src, const language_binding_config& cfg, language_binding_state& state, const std::string_view& f_name){ + 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, src, cfg, state); + if(eov.is_error()){ + return eov; + } + } + { + auto eov = lang_bind<Output, binding::C>::generate(buff, src, cfg, state); + if(eov.is_error()){ + return eov; + } + } { auto eov = lang_bind_helper::append_string(buff, "typedef "); if(eov.is_error()){ @@ -342,7 +381,7 @@ struct lang_bind<schema::Function<Input, Output>, binding::C> { } } { - auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash<Output>::apply()); + auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash<Input>::apply()); if(eov.is_error()){ return eov; } @@ -372,61 +411,61 @@ struct lang_bind<schema::Function<Input, Output>, binding::C> { } } { - auto eov = lang_bind_helper::append_string(buff, "_output_t;\n"); + auto eov = lang_bind_helper::append_string(buff, "_input_t;\n"); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, "int "); + auto eov = lang_bind_helper::append_string(buff, "typedef "); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, cfg.prefix); + auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash<Output>::apply()); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, "_"); + auto eov = lang_bind_helper::append_string(buff, " "); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, f_name); + auto eov = lang_bind_helper::append_string(buff, cfg.prefix); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, "( const "); + 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<Input>::apply()); + auto eov = lang_bind_helper::append_string(buff, f_name); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, "* input, "); + auto eov = lang_bind_helper::append_string(buff, "_output_t;\n"); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_hashed_type(buff, cfg.prefix, schema_hash<Output>::apply()); + auto eov = append_function_def(buff, cfg, state, f_name); if(eov.is_error()){ return eov; } } { - auto eov = lang_bind_helper::append_string(buff, "* output);\n\n"); + auto eov = lang_bind_helper::append_string(buff, ";\n\n"); if(eov.is_error()){ return eov; } @@ -439,25 +478,25 @@ struct lang_bind<schema::Function<Input, Output>, binding::C> { 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){ + static error_or<void> generate_element(buffer& buff, buffer& src, 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, MKey.view()); + auto eov = lang_bind<MValue, binding::C>::generate(buff, src, cfg, state, MKey.view()); if(eov.is_error()){ return eov; } } if constexpr ((i+1) < sizeof...(M) ){ - return generate_element<i+1>(buff, cfg, state); + return generate_element<i+1>(buff, src, cfg, state); } return void_t{}; } - static error_or<void> generate(buffer& buff, const language_binding_config& cfg){ + static error_or<void> generate(buffer& buff, buffer& src, const language_binding_config& cfg){ if(cfg.prefix.size() == 0){ return make_error<err::invalid_state>("C interfaces need a prefix."); } @@ -465,7 +504,7 @@ struct lang_bind<schema::Interface<M...>, binding::C> { language_binding_state state; if constexpr (sizeof...(M) > 0){ - return generate_element<0>(buff, cfg, state); + return generate_element<0>(buff, src, cfg, state); } return void_t{}; diff --git a/modules/tools/tests/c_iface.cpp b/modules/tools/tests/c_iface.cpp index 61668de..29cc7f9 100644 --- a/modules/tools/tests/c_iface.cpp +++ b/modules/tools/tests/c_iface.cpp @@ -45,24 +45,27 @@ using TestMultiFunctionInterface = Interface< } template<typename Schema> -void test_generate(std::string& res){ +void test_generate(std::string& res, std::string& src){ using namespace saw; ring_buffer r_buff{4u * 1024u * 1024u}; + ring_buffer r_src_buff{4u * 1024u * 1024u}; { - auto eov = language_binding<Schema, binding::C>::generate(r_buff, {"prefix"}); + auto eov = language_binding<Schema, binding::C>::generate(r_buff, r_src_buff, {"prefix"}); SAW_EXPECT(eov.is_value(), std::string{"Couldn't generate interface info: "} + std::string{eov.get_error().get_message()}); } res = convert_to_string(r_buff); + src = convert_to_string(r_src_buff); } SAW_TEST("CIface Empty Interface"){ using namespace saw; std::string res; - test_generate<schema::TestEmptyInterface>(res); + std::string src; + test_generate<schema::TestEmptyInterface>(res,src); std::cout<<"\n"<<res<<"\n"<<std::endl; } @@ -71,7 +74,8 @@ SAW_TEST("CIface One Function Interface"){ using namespace saw; std::string res; - test_generate<schema::TestOneFunctionInterface>(res); + std::string src; + test_generate<schema::TestOneFunctionInterface>(res, src); std::cout<<"\n"<<res<<"\n"<<std::endl; } @@ -79,7 +83,8 @@ SAW_TEST("CIface Multi Function Interface"){ using namespace saw; std::string res; - test_generate<schema::TestMultiFunctionInterface>(res); + std::string src; + test_generate<schema::TestMultiFunctionInterface>(res, src); std::cout<<"\n"<<res<<"\n"<<std::endl; } @@ -87,7 +92,8 @@ SAW_TEST("CIface Array Function Interface"){ using namespace saw; std::string res; - test_generate<schema::TestArrayFunctionInterface>(res); + std::string src; + test_generate<schema::TestArrayFunctionInterface>(res, src); std::cout<<"\n"<<res<<"\n"<<std::endl; } } |