From a4ae92d8dd2d937f5242ed85d8a8fda4912e1a34 Mon Sep 17 00:00:00 2001 From: Lauro Moura Date: Tue, 26 Mar 2019 14:45:18 -0300 Subject: [PATCH] efl-csharp: Remove spaces from type strings in generator. Summary: Previously, the type_impl and marshall_type_impl generators relied on a type mismatch in the match table to fallback to the else branch in the match check to actually print the type string. This was achieved by adding the " " prefix to the type. This commit changes this behavior to invoke a proper visitor just to print and makes both generators return trimmed type strings. This will help conforming to the C# coding conventions. Test Plan: run test suite Reviewers: felipealmeida, vitor.sousa Reviewed By: felipealmeida, vitor.sousa Subscribers: cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D8441 --- .../eolian/mono/function_definition.hh | 4 +- .../eolian/mono/function_pointer.hh | 2 +- .../eolian/mono/marshall_type_impl.hh | 50 ++++---- src/bin/eolian_mono/eolian/mono/type_impl.hh | 119 ++++++++++-------- 4 files changed, 98 insertions(+), 77 deletions(-) diff --git a/src/bin/eolian_mono/eolian/mono/function_definition.hh b/src/bin/eolian_mono/eolian/mono/function_definition.hh index 465a8fee71..fe349effab 100644 --- a/src/bin/eolian_mono/eolian/mono/function_definition.hh +++ b/src/bin/eolian_mono/eolian/mono/function_definition.hh @@ -107,7 +107,7 @@ struct native_function_definition_generator << scope_tab << scope_tab << "if(wrapper != null) {\n" << scope_tab << scope_tab << scope_tab << eolian_mono::native_function_definition_preamble() << scope_tab << scope_tab << scope_tab << "try {\n" - << scope_tab << scope_tab << scope_tab << scope_tab << (return_type != " void" ? "_ret_var = " : "") + << scope_tab << scope_tab << scope_tab << scope_tab << (return_type != "void" ? "_ret_var = " : "") << (f.is_static ? "" : "((") << klass_cast_name << (f.is_static ? "." : ")wrapper).") << string << "(" << (native_argument_invocation % ", ") << ");\n" << scope_tab << scope_tab << scope_tab << "} catch (Exception e) {\n" @@ -116,7 +116,7 @@ struct native_function_definition_generator << scope_tab << scope_tab << scope_tab << "}\n" << eolian_mono::native_function_definition_epilogue(*klass) << scope_tab << scope_tab << "} else {\n" - << scope_tab << scope_tab << scope_tab << (return_type != " void" ? "return " : "") << string + << scope_tab << scope_tab << scope_tab << (return_type != "void" ? "return " : "") << string << "_ptr.Value.Delegate(" << self << ((!f.is_static && f.parameters.size() > 0) ? ", " : "") << (argument % ", ") << ");\n" << scope_tab << scope_tab << "}\n" << scope_tab << "}\n" diff --git a/src/bin/eolian_mono/eolian/mono/function_pointer.hh b/src/bin/eolian_mono/eolian/mono/function_pointer.hh index f3918f1c26..d616f1da2a 100644 --- a/src/bin/eolian_mono/eolian/mono/function_pointer.hh +++ b/src/bin/eolian_mono/eolian/mono/function_pointer.hh @@ -89,7 +89,7 @@ struct function_pointer { << scope_tab << scope_tab << string << " cb = (" << string << ")handle.Target;\n" << native_function_definition_preamble << scope_tab << scope_tab << "try {\n" - << scope_tab << scope_tab << scope_tab << (return_type != " void" ? "_ret_var = " : "") << "cb(" << (native_argument_invocation % ", ") << ");\n" + << scope_tab << scope_tab << scope_tab << (return_type != "void" ? "_ret_var = " : "") << "cb(" << (native_argument_invocation % ", ") << ");\n" << scope_tab << scope_tab << "} catch (Exception e) {\n" << scope_tab << scope_tab << scope_tab << "Eina.Log.Warning($\"Callback error: {e.ToString()}\");\n" << scope_tab << scope_tab << scope_tab << "Eina.Error.Set(Eina.Error.UNHANDLED_EXCEPTION);\n" diff --git a/src/bin/eolian_mono/eolian/mono/marshall_type_impl.hh b/src/bin/eolian_mono/eolian/mono/marshall_type_impl.hh index 998ca0bdb3..ae619ea19e 100644 --- a/src/bin/eolian_mono/eolian/mono/marshall_type_impl.hh +++ b/src/bin/eolian_mono/eolian/mono/marshall_type_impl.hh @@ -46,55 +46,55 @@ struct marshall_type_visitor_generate { regular_type_def r = regular; r.base_qualifier.qualifier ^= qualifier_info::is_ref; - return replace_base_type(r, " System.String"); + return replace_base_type(r, "System.String"); }} , {"string", false, [&] { regular_type_def r = regular; r.base_qualifier.qualifier ^= qualifier_info::is_ref; - return replace_base_type(r, " System.String"); + return replace_base_type(r, "System.String"); }} , {"mstring", true, [&] { regular_type_def r = regular; r.base_qualifier.qualifier ^= qualifier_info::is_ref; - return replace_base_type(r, " System.String"); + return replace_base_type(r, "System.String"); }} , {"mstring", false, [&] { regular_type_def r = regular; r.base_qualifier.qualifier ^= qualifier_info::is_ref; - return replace_base_type(r, " System.String"); + return replace_base_type(r, "System.String"); }} , {"stringshare", true, [&] { regular_type_def r = regular; r.base_qualifier.qualifier ^= qualifier_info::is_ref; - return replace_base_type(r, " System.String"); + return replace_base_type(r, "System.String"); }} , {"stringshare", false, [&] { regular_type_def r = regular; r.base_qualifier.qualifier ^= qualifier_info::is_ref; - return replace_base_type(r, " System.String"); + return replace_base_type(r, "System.String"); }} , {"strbuf", nullptr, [&] { regular_type_def r = regular; r.base_qualifier.qualifier ^= qualifier_info::is_ref; - return replace_base_type(r, " Eina.Strbuf"); + return replace_base_type(r, "Eina.Strbuf"); }} , {"Binbuf", true, [&] { regular_type_def r = regular; - r.base_type = " System.IntPtr"; + r.base_type = "System.IntPtr"; r.namespaces.clear(); return r; }} , {"Binbuf", false, [&] { regular_type_def r = regular; - r.base_type = " System.IntPtr"; + r.base_type = "System.IntPtr"; r.namespaces.clear(); return r; }} @@ -103,9 +103,9 @@ struct marshall_type_visitor_generate regular_type_def r = regular; r.namespaces.clear(); if (is_ptr) - r.base_type = " Eina.Value"; + r.base_type = "Eina.Value"; else - r.base_type = " Eina.ValueNative"; + r.base_type = "Eina.ValueNative"; return r; }} , {"any_value", false, [&] @@ -113,23 +113,23 @@ struct marshall_type_visitor_generate regular_type_def r = regular; r.namespaces.clear(); if (is_ptr) - r.base_type = " Eina.Value"; + r.base_type = "Eina.Value"; else - r.base_type = " Eina.ValueNative"; + r.base_type = "Eina.ValueNative"; return r; }} , {"any_value_ptr", true, [&] { regular_type_def r = regular; r.namespaces.clear(); - r.base_type = " Eina.Value"; + r.base_type = "Eina.Value"; return r; }} , {"any_value_ptr", false, [&] { regular_type_def r = regular; r.namespaces.clear(); - r.base_type = " Eina.Value"; + r.base_type = "Eina.Value"; return r; }} , {"void", nullptr, [&] @@ -137,9 +137,9 @@ struct marshall_type_visitor_generate regular_type_def r = regular; r.namespaces.clear(); if (is_out) // @inout too - r.base_type = " System.IntPtr"; + r.base_type = "System.IntPtr"; else - r.base_type = " void"; + r.base_type = "void"; return r; }} }; @@ -147,7 +147,7 @@ struct marshall_type_visitor_generate if (regular.is_struct() && !blacklist::is_struct_blacklisted(regular) && !(bool)(regular.base_qualifier & qualifier_info::is_own)) { if ((is_out || is_return) && is_ptr) - return as_generator(" System.IntPtr").generate(sink, attributes::unused, *context); + return as_generator("System.IntPtr").generate(sink, attributes::unused, *context); return as_generator(string << "_StructInternal") .generate(sink, name_helpers::type_full_managed_name(regular), *context); } @@ -161,7 +161,7 @@ struct marshall_type_visitor_generate } , [&] (attributes::type_def::variant_type const& v) { - return v.visit(*this); // we want to keep is_out info + return v.visit(visitor_regular_type_def_printer{sink, context}); // we want to keep is_out info })) { return *b; @@ -169,7 +169,7 @@ struct marshall_type_visitor_generate else if (is_ptr && helpers::need_pointer_conversion(®ular)) { regular_type_def r = regular; - r.base_type = " System.IntPtr"; + r.base_type = "System.IntPtr"; r.namespaces.clear(); return visitor_generate{sink, context, c_type, is_out, is_return, is_ptr}(r); } @@ -196,27 +196,27 @@ struct marshall_type_visitor_generate { {"array", nullptr, nullptr, [&] { - return regular_type_def{" System.IntPtr", complex.outer.base_qualifier, {}}; + return regular_type_def{"System.IntPtr", complex.outer.base_qualifier, {}}; } } ,{"list", nullptr, nullptr, [&] { - return regular_type_def{" System.IntPtr", complex.outer.base_qualifier, {}}; + return regular_type_def{"System.IntPtr", complex.outer.base_qualifier, {}}; } } ,{"hash", nullptr, nullptr, [&] { - return regular_type_def{" System.IntPtr", complex.outer.base_qualifier, {}}; + return regular_type_def{"System.IntPtr", complex.outer.base_qualifier, {}}; } } ,{"iterator", nullptr, nullptr, [&] { - return regular_type_def{" System.IntPtr", complex.outer.base_qualifier, {}}; + return regular_type_def{"System.IntPtr", complex.outer.base_qualifier, {}}; } } ,{"accessor", nullptr, nullptr, [&] { - return regular_type_def{" System.IntPtr", complex.outer.base_qualifier, {}}; + return regular_type_def{"System.IntPtr", complex.outer.base_qualifier, {}}; } } }; diff --git a/src/bin/eolian_mono/eolian/mono/type_impl.hh b/src/bin/eolian_mono/eolian/mono/type_impl.hh index f34c4796ec..4b3f097dd4 100644 --- a/src/bin/eolian_mono/eolian/mono/type_impl.hh +++ b/src/bin/eolian_mono/eolian/mono/type_impl.hh @@ -26,10 +26,10 @@ attributes::regular_type_def replace_base_integer(attributes::regular_type_def v bool s = std::is_signed::value; switch (sizeof(T)) { - case 1: return s ? replace_base_type(v, " sbyte") : replace_base_type(v, " byte"); - case 2: return s ? replace_base_type(v, " short") : replace_base_type(v, " ushort"); - case 4: return s ? replace_base_type(v, " int") : replace_base_type(v, " uint"); - case 8: return s ? replace_base_type(v, " long") : replace_base_type(v, " ulong"); + case 1: return s ? replace_base_type(v, "sbyte") : replace_base_type(v, "byte"); + case 2: return s ? replace_base_type(v, "short") : replace_base_type(v, "ushort"); + case 4: return s ? replace_base_type(v, "int") : replace_base_type(v, "uint"); + case 8: return s ? replace_base_type(v, "long") : replace_base_type(v, "ulong"); default: return v; } } @@ -40,10 +40,10 @@ attributes::regular_type_def replace_base_opt_integer(attributes::regular_type_d bool s = std::is_signed::value; switch (sizeof(T)) { - case 1: return s ? replace_base_type(v, " sbyte?") : replace_base_type(v, " byte?"); - case 2: return s ? replace_base_type(v, " short?") : replace_base_type(v, " ushort?"); - case 4: return s ? replace_base_type(v, " int?") : replace_base_type(v, " uint?"); - case 8: return s ? replace_base_type(v, " long?") : replace_base_type(v, " ulong?"); + case 1: return s ? replace_base_type(v, "sbyte?") : replace_base_type(v, "byte?"); + case 2: return s ? replace_base_type(v, "short?") : replace_base_type(v, "ushort?"); + case 4: return s ? replace_base_type(v, "int?") : replace_base_type(v, "uint?"); + case 8: return s ? replace_base_type(v, "long?") : replace_base_type(v, "ulong?"); default: return v; } } @@ -67,7 +67,28 @@ eina::optional call_match(Array const (&array)[N], F f, A a) } return {nullptr}; } - + +template +struct visitor_regular_type_def_printer +{ + typedef visitor_regular_type_def_printer visitor_type; + typedef bool result_type; + + mutable OutputIterator sink; + Context const* context; + + bool operator()(grammar::attributes::regular_type_def const ®ular) const + { + return as_generator(string).generate(sink, name_helpers::type_full_managed_name(regular), *context); + } + + template + bool operator()(T const&) const + { + return true; + } +}; + template struct visitor_generate { @@ -94,110 +115,110 @@ struct visitor_generate const optional_match_table[] = { // signed primitives - {"byte", nullptr, [&] { return replace_base_type(regular, " sbyte?"); }} - , {"float", nullptr, [&] { return replace_base_type(regular, " float?"); }} - , {"double", nullptr, [&] { return replace_base_type(regular, " double?"); }} - , {"bool", nullptr, [&] { return replace_base_type(regular, " bool?"); }} + {"byte", nullptr, [&] { return replace_base_type(regular, "sbyte?"); }} + , {"float", nullptr, [&] { return replace_base_type(regular, "float?"); }} + , {"double", nullptr, [&] { return replace_base_type(regular, "double?"); }} + , {"bool", nullptr, [&] { return replace_base_type(regular, "bool?"); }} , {"short", nullptr, [&] { return replace_base_opt_integer(regular); }} , {"int", nullptr, [&] { return replace_base_opt_integer(regular); }} , {"long", nullptr, [&] { return replace_base_opt_integer(regular); }} , {"llong", nullptr, [&] { return replace_base_opt_integer(regular); }} - , {"int8", nullptr, [&] { return replace_base_type(regular, " sbyte?"); }} - , {"int16", nullptr, [&] { return replace_base_type(regular, " short?"); }} - , {"int32", nullptr, [&] { return replace_base_type(regular, " int?"); }} - , {"int64", nullptr, [&] { return replace_base_type(regular, " long?"); }} + , {"int8", nullptr, [&] { return replace_base_type(regular, "sbyte?"); }} + , {"int16", nullptr, [&] { return replace_base_type(regular, "short?"); }} + , {"int32", nullptr, [&] { return replace_base_type(regular, "int?"); }} + , {"int64", nullptr, [&] { return replace_base_type(regular, "long?"); }} , {"ssize", nullptr, [&] { return replace_base_opt_integer(regular); }} // unsigned primitives - , {"ubyte", nullptr, [&] { return replace_base_type(regular, " byte?"); }} + , {"ubyte", nullptr, [&] { return replace_base_type(regular, "byte?"); }} , {"ushort", nullptr, [&] { return replace_base_opt_integer(regular); }} , {"uint", nullptr, [&] { return replace_base_opt_integer(regular); }} , {"ulong", nullptr, [&] { return replace_base_opt_integer(regular); }} , {"ullong", nullptr, [&] { return replace_base_opt_integer(regular); }} - , {"uint8", nullptr, [&] { return replace_base_type(regular, " byte?"); }} - , {"uint16", nullptr, [&] { return replace_base_type(regular, " ushort?"); }} - , {"uint32", nullptr, [&] { return replace_base_type(regular, " uint?"); }} - , {"uint64", nullptr, [&] { return replace_base_type(regular, " ulong?"); }} + , {"uint8", nullptr, [&] { return replace_base_type(regular, "byte?"); }} + , {"uint16", nullptr, [&] { return replace_base_type(regular, "ushort?"); }} + , {"uint32", nullptr, [&] { return replace_base_type(regular, "uint?"); }} + , {"uint64", nullptr, [&] { return replace_base_type(regular, "ulong?"); }} , {"size", nullptr, [&] { return replace_base_opt_integer(regular); }} , {"ptrdiff", nullptr, [&] { return replace_base_opt_integer(regular); }} - , {"intptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr?"); }} - , {"uintptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr?"); }} - , {"void_ptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr?"); }} + , {"intptr", nullptr, [&] { return replace_base_type(regular, "System.IntPtr?"); }} + , {"uintptr", nullptr, [&] { return replace_base_type(regular, "System.IntPtr?"); }} + , {"void_ptr", nullptr, [&] { return replace_base_type(regular, "System.IntPtr?"); }} }; struct match const match_table[] = { // signed primitives - {"byte", nullptr, [&] { return replace_base_type(regular, " sbyte"); }} + {"byte", nullptr, [&] { return replace_base_type(regular, "sbyte"); }} , {"short", nullptr, [&] { return replace_base_integer(regular); }} , {"int", nullptr, [&] { return replace_base_integer(regular); }} , {"long", nullptr, [&] { return replace_base_integer(regular); }} , {"llong", nullptr, [&] { return replace_base_integer(regular); }} - , {"int8", nullptr, [&] { return replace_base_type(regular, " sbyte"); }} - , {"int16", nullptr, [&] { return replace_base_type(regular, " short"); }} - , {"int32", nullptr, [&] { return replace_base_type(regular, " int"); }} - , {"int64", nullptr, [&] { return replace_base_type(regular, " long"); }} + , {"int8", nullptr, [&] { return replace_base_type(regular, "sbyte"); }} + , {"int16", nullptr, [&] { return replace_base_type(regular, "short"); }} + , {"int32", nullptr, [&] { return replace_base_type(regular, "int"); }} + , {"int64", nullptr, [&] { return replace_base_type(regular, "long"); }} , {"ssize", nullptr, [&] { return replace_base_integer(regular); }} // unsigned primitives - , {"ubyte", nullptr, [&] { return replace_base_type(regular, " byte"); }} + , {"ubyte", nullptr, [&] { return replace_base_type(regular, "byte"); }} , {"ushort", nullptr, [&] { return replace_base_integer(regular); }} , {"uint", nullptr, [&] { return replace_base_integer(regular); }} , {"ulong", nullptr, [&] { return replace_base_integer(regular); }} , {"ullong", nullptr, [&] { return replace_base_integer(regular); }} - , {"uint8", nullptr, [&] { return replace_base_type(regular, " byte"); }} - , {"uint16", nullptr, [&] { return replace_base_type(regular, " ushort"); }} - , {"uint32", nullptr, [&] { return replace_base_type(regular, " uint"); }} - , {"uint64", nullptr, [&] { return replace_base_type(regular, " ulong"); }} + , {"uint8", nullptr, [&] { return replace_base_type(regular, "byte"); }} + , {"uint16", nullptr, [&] { return replace_base_type(regular, "ushort"); }} + , {"uint32", nullptr, [&] { return replace_base_type(regular, "uint"); }} + , {"uint64", nullptr, [&] { return replace_base_type(regular, "ulong"); }} , {"size", nullptr, [&] { return replace_base_integer(regular); }} , {"ptrdiff", nullptr, [&] { return replace_base_integer(regular); }} - , {"intptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr"); }} - , {"uintptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr"); }} - , {"void_ptr", nullptr, [&] { return replace_base_type(regular, " System.IntPtr"); }} + , {"intptr", nullptr, [&] { return replace_base_type(regular, "System.IntPtr"); }} + , {"uintptr", nullptr, [&] { return replace_base_type(regular, "System.IntPtr"); }} + , {"void_ptr", nullptr, [&] { return replace_base_type(regular, "System.IntPtr"); }} , {"void", nullptr, [&] { regular_type_def r = regular; r.namespaces.clear(); if (is_out) // @inout too - r.base_type = " System.IntPtr"; + r.base_type = "System.IntPtr"; else - r.base_type = " void"; + r.base_type = "void"; return r; }} , {"Eina.Error", nullptr, [&] // Eina.Error { - return regular_type_def{" Eina.Error", regular.base_qualifier, {}}; + return regular_type_def{"Eina.Error", regular.base_qualifier, {}}; }} // TODO , {"string", nullptr, [&] { regular_type_def r = regular; r.base_qualifier.qualifier ^= qualifier_info::is_ref; - return replace_base_type(r, " System.String"); + return replace_base_type(r, "System.String"); }} , {"mstring", nullptr, [&] { regular_type_def r = regular; r.base_qualifier.qualifier ^= qualifier_info::is_ref; - return replace_base_type(r, " System.String"); + return replace_base_type(r, "System.String"); }} , {"stringshare", nullptr, [&] { regular_type_def r = regular; r.base_qualifier.qualifier ^= qualifier_info::is_ref; - return replace_base_type(r, " System.String"); + return replace_base_type(r, "System.String"); }} , {"strbuf", nullptr, [&] { - return regular_type_def{" Eina.Strbuf", regular.base_qualifier, {}}; + return regular_type_def{"Eina.Strbuf", regular.base_qualifier, {}}; }} , {"any_value", true, [&] - { return regular_type_def{" Eina.Value", regular.base_qualifier, {}}; + { return regular_type_def{"Eina.Value", regular.base_qualifier, {}}; }} , {"any_value", false, [&] - { return regular_type_def{" Eina.Value", regular.base_qualifier, {}}; + { return regular_type_def{"Eina.Value", regular.base_qualifier, {}}; }} , {"any_value_ptr", nullptr, [&] - { return regular_type_def{" Eina.Value", regular.base_qualifier, {}}; + { return regular_type_def{"Eina.Value", regular.base_qualifier, {}}; }} // FIXME add proper support for any_value_ptr }; std::string full_type_name = name_helpers::type_full_eolian_name(regular); @@ -234,7 +255,7 @@ struct visitor_generate } , [&] (attributes::type_def::variant_type const& v) { - return v.visit(*this); // we want to keep is_out info + return v.visit(visitor_regular_type_def_printer{sink, context}); // we want to keep is_out info })) { return *b;