From a4c4d27ad62050a5bbea680654a63fc487ba6d9a Mon Sep 17 00:00:00 2001 From: Vitor Sousa Date: Fri, 22 Mar 2019 18:37:50 -0300 Subject: [PATCH] eolian_csharp: add indentation context Summary: Also, use new context class for cleaner constructs. Also, make functions receive context objects by reference to avoid unnecessary object copies (since context objects are bigger now). This commit contains preparation structures for a future overhaul of white space generation. Depends on D8467 Test Plan: ninja test Reviewers: felipealmeida, lauromoura Reviewed By: lauromoura Subscribers: cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D8468 --- src/bin/eolian_mono/eolian/mono/blacklist.hh | 16 ++++++------ src/bin/eolian_mono/eolian/mono/events.hh | 6 ++--- .../eolian_mono/eolian/mono/function_definition.hh | 2 +- .../eolian_mono/eolian/mono/function_pointer.hh | 2 +- .../eolian_mono/eolian/mono/generation_contexts.hh | 30 ++++++++++++++++++++++ src/bin/eolian_mono/eolian/mono/helpers.hh | 2 +- src/bin/eolian_mono/eolian/mono/name_helpers.hh | 4 +-- src/bin/eolian_mono/eolian/mono/parameter.hh | 4 +-- .../eolian_mono/eolian/mono/variable_definition.hh | 2 +- src/bin/eolian_mono/eolian_mono.cc | 19 +++++++------- 10 files changed, 59 insertions(+), 28 deletions(-) diff --git a/src/bin/eolian_mono/eolian/mono/blacklist.hh b/src/bin/eolian_mono/eolian/mono/blacklist.hh index 77be1c0..df94b6a 100644 --- a/src/bin/eolian_mono/eolian/mono/blacklist.hh +++ b/src/bin/eolian_mono/eolian/mono/blacklist.hh @@ -52,7 +52,7 @@ inline bool is_function_blacklisted(std::string const& c_name) } template -inline bool is_function_blacklisted(attributes::function_def const& func, Context context) +inline bool is_function_blacklisted(attributes::function_def const& func, Context const& context) { auto options = efl::eolian::grammar::context_find_tag(context); auto c_name = func.c_name; @@ -80,7 +80,7 @@ inline bool is_struct_blacklisted(std::string const& full_name) } template -inline bool is_struct_blacklisted(attributes::struct_def const& struct_, Context context) +inline bool is_struct_blacklisted(attributes::struct_def const& struct_, Context const& context) { auto options = efl::eolian::grammar::context_find_tag(context); if (struct_.is_beta && !options.want_beta) @@ -91,7 +91,7 @@ inline bool is_struct_blacklisted(attributes::struct_def const& struct_, Context // Struct as type_def is for places where the struct is used as a struct field or parameter/return. template -inline bool is_struct_blacklisted(attributes::type_def const& struct_, Context context) +inline bool is_struct_blacklisted(attributes::type_def const& struct_, Context const& context) { auto options = efl::eolian::grammar::context_find_tag(context); if (struct_.is_beta && !options.want_beta) @@ -119,7 +119,7 @@ inline bool is_property_blacklisted(std::string const& name) } template -inline bool is_property_blacklisted(attributes::property_def const& property, Context context) +inline bool is_property_blacklisted(attributes::property_def const& property, Context const& context) { auto name = name_helpers::klass_full_concrete_or_interface_name(property.klass) + "." + name_helpers::property_managed_name(property); @@ -135,7 +135,7 @@ inline bool is_property_blacklisted(attributes::property_def const& property, Co template inline bool is_property_blacklisted(attributes::property_def const& property, attributes::klass_def const& implementing_class, - Context context) + Context const& context) { std::string property_name = name_helpers::property_managed_name(property); std::string klass_name = name_helpers::klass_concrete_or_interface_name(implementing_class); @@ -149,7 +149,7 @@ inline bool is_property_blacklisted(attributes::property_def const& property, } template -inline bool is_class_blacklisted(attributes::klass_def const& cls, Context context) +inline bool is_class_blacklisted(attributes::klass_def const& cls, Context const& context) { auto options = efl::eolian::grammar::context_find_tag(context); @@ -157,7 +157,7 @@ inline bool is_class_blacklisted(attributes::klass_def const& cls, Context conte } template -inline bool is_class_blacklisted(attributes::klass_name const& cls, Context context) +inline bool is_class_blacklisted(attributes::klass_name const& cls, Context const& context) { auto options = efl::eolian::grammar::context_find_tag(context); @@ -166,7 +166,7 @@ inline bool is_class_blacklisted(attributes::klass_name const& cls, Context cont template -inline bool is_event_blacklisted(attributes::event_def const& evt, Context context) +inline bool is_event_blacklisted(attributes::event_def const& evt, Context const& context) { auto options = efl::eolian::grammar::context_find_tag(context); diff --git a/src/bin/eolian_mono/eolian/mono/events.hh b/src/bin/eolian_mono/eolian/mono/events.hh index f4f6987..1a3aca1 100644 --- a/src/bin/eolian_mono/eolian/mono/events.hh +++ b/src/bin/eolian_mono/eolian/mono/events.hh @@ -179,7 +179,7 @@ struct event_definition_generator bool is_inherited_event; template - bool generate(OutputIterator sink, attributes::event_def const& evt, Context context) const + bool generate(OutputIterator sink, attributes::event_def const& evt, Context const& context) const { if (blacklist::is_event_blacklisted(evt, context)) return true; @@ -291,7 +291,7 @@ struct event_definition_generator , std::string const& event_name , std::string const& event_args_type , std::string const& event_template_args - , Context context) const + , Context const& context) const { auto delegate_type = "EventHandler" + event_template_args; if (!as_generator( @@ -311,7 +311,7 @@ struct event_definition_generator } template - bool generate_event_add_remove(OutputIterator sink, attributes::event_def const &evt, const std::string& event_name, Context context) const + bool generate_event_add_remove(OutputIterator sink, attributes::event_def const &evt, const std::string& event_name, Context const& context) const { std::string upper_c_name = utils::to_uppercase(evt.c_name); auto unit = (const Eolian_Unit*) context_find_tag(context).state; diff --git a/src/bin/eolian_mono/eolian/mono/function_definition.hh b/src/bin/eolian_mono/eolian/mono/function_definition.hh index fe349ef..0d6eff6 100644 --- a/src/bin/eolian_mono/eolian/mono/function_definition.hh +++ b/src/bin/eolian_mono/eolian/mono/function_definition.hh @@ -217,7 +217,7 @@ struct native_function_definition_parameterized struct property_wrapper_definition_generator { template - bool generate(OutputIterator sink, attributes::property_def const& property, Context context) const + bool generate(OutputIterator sink, attributes::property_def const& property, Context const& context) const { if (blacklist::is_property_blacklisted(property, *implementing_klass, context)) return true; diff --git a/src/bin/eolian_mono/eolian/mono/function_pointer.hh b/src/bin/eolian_mono/eolian/mono/function_pointer.hh index d616f1d..aa99601 100644 --- a/src/bin/eolian_mono/eolian/mono/function_pointer.hh +++ b/src/bin/eolian_mono/eolian/mono/function_pointer.hh @@ -15,7 +15,7 @@ namespace eolian_mono { // Blacklist structs that require some kind of manual binding. template -static bool is_function_ptr_blacklisted(attributes::function_def const& func, Context context) +static bool is_function_ptr_blacklisted(attributes::function_def const& func, Context const& context) { std::string name = name_helpers::function_ptr_full_eolian_name(func); diff --git a/src/bin/eolian_mono/eolian/mono/generation_contexts.hh b/src/bin/eolian_mono/eolian/mono/generation_contexts.hh index 5cc57ef..25ac309 100644 --- a/src/bin/eolian_mono/eolian/mono/generation_contexts.hh +++ b/src/bin/eolian_mono/eolian/mono/generation_contexts.hh @@ -1,6 +1,9 @@ #ifndef EOLIAN_MONO_GENERATION_CONTEXTS_HH #define EOLIAN_MONO_GENERATION_CONTEXTS_HH +#include "grammar/context.hpp" +#include "grammar/indentation.hpp" + namespace eolian_mono { struct class_context @@ -19,6 +22,33 @@ struct class_context wrapper_kind current_wrapper_kind; }; +struct indentation_context +{ + constexpr indentation_context(indentation_context const& other) = default; + constexpr indentation_context(efl::eolian::grammar::scope_tab_generator indent) + : indent(indent) + {} + constexpr indentation_context(int n) + : indent(n) + {} + constexpr indentation_context(int n, int m) + : indent(n, m) + {} + efl::eolian::grammar::scope_tab_generator indent; +}; + +template +inline constexpr efl::eolian::grammar::scope_tab_generator const& current_indentation(Context const& context) +{ + return efl::eolian::grammar::context_find_tag(context).indent; +} + +template +inline constexpr Context change_indentation(efl::eolian::grammar::scope_tab_generator const& indent, Context const& context) +{ + return efl::eolian::grammar::context_replace_tag(indentation_context(indent), context); +} + struct library_context { std::string library_name; diff --git a/src/bin/eolian_mono/eolian/mono/helpers.hh b/src/bin/eolian_mono/eolian/mono/helpers.hh index a4bb169..dffb38b 100644 --- a/src/bin/eolian_mono/eolian/mono/helpers.hh +++ b/src/bin/eolian_mono/eolian/mono/helpers.hh @@ -103,7 +103,7 @@ std::set interfa // Returns the set of interfaces implemented by this type that haven't been implemented // by a regular parent class. template -std::set non_implemented_interfaces(attributes::klass_def const& cls, Context context) +std::set non_implemented_interfaces(attributes::klass_def const& cls, Context const& context) { auto options = efl::eolian::grammar::context_find_tag(context); std::set implemented_interfaces; diff --git a/src/bin/eolian_mono/eolian/mono/name_helpers.hh b/src/bin/eolian_mono/eolian/mono/name_helpers.hh index 6989764..2ff2b47 100644 --- a/src/bin/eolian_mono/eolian/mono/name_helpers.hh +++ b/src/bin/eolian_mono/eolian/mono/name_helpers.hh @@ -442,7 +442,7 @@ inline std::string translate_inherited_event_name(const attributes::event_def &e // Open/close namespaces template -bool open_namespaces(OutputIterator sink, std::vector namespaces, Context context) +bool open_namespaces(OutputIterator sink, std::vector namespaces, Context const& context) { std::transform(namespaces.begin(), namespaces.end(), namespaces.begin(), managed_namespace); @@ -451,7 +451,7 @@ bool open_namespaces(OutputIterator sink, std::vector namespaces, C } template -bool close_namespaces(OutputIterator sink, std::vector const& namespaces, Context context) +bool close_namespaces(OutputIterator sink, std::vector const& namespaces, Context const& context) { auto close_namespace = *(lit("} ")) << "\n"; return as_generator(close_namespace).generate(sink, namespaces, context); diff --git a/src/bin/eolian_mono/eolian/mono/parameter.hh b/src/bin/eolian_mono/eolian/mono/parameter.hh index 1314c12..730fa66 100644 --- a/src/bin/eolian_mono/eolian/mono/parameter.hh +++ b/src/bin/eolian_mono/eolian/mono/parameter.hh @@ -1452,7 +1452,7 @@ struct constructor_parameter_name_parameterized struct constructor_param_generator { template - bool generate(OutputIterator sink, attributes::constructor_def const& ctor, Context context) const + bool generate(OutputIterator sink, attributes::constructor_def const& ctor, Context const& context) const { auto params = ctor.function.parameters; @@ -1471,7 +1471,7 @@ struct constructor_param_generator struct constructor_invocation_generator { template - bool generate(OutputIterator sink, attributes::constructor_def const& ctor, Context context) const + bool generate(OutputIterator sink, attributes::constructor_def const& ctor, Context const& context) const { auto params = ctor.function.parameters; if (!as_generator( diff --git a/src/bin/eolian_mono/eolian/mono/variable_definition.hh b/src/bin/eolian_mono/eolian/mono/variable_definition.hh index d143d7e..f13efde 100644 --- a/src/bin/eolian_mono/eolian/mono/variable_definition.hh +++ b/src/bin/eolian_mono/eolian/mono/variable_definition.hh @@ -28,7 +28,7 @@ namespace eolian_mono { struct constant_definition_generator { template - bool generate(OutputIterator sink, attributes::variable_def constant, Context context) const + bool generate(OutputIterator sink, attributes::variable_def constant, Context const& context) const { // Open partial class if (!name_helpers::open_namespaces(sink, constant.namespaces, context)) diff --git a/src/bin/eolian_mono/eolian_mono.cc b/src/bin/eolian_mono/eolian_mono.cc index e3cbe5f..0699c77 100644 --- a/src/bin/eolian_mono/eolian_mono.cc +++ b/src/bin/eolian_mono/eolian_mono.cc @@ -140,15 +140,16 @@ run(options_type const& opts) throw std::runtime_error("Failed to generate file preamble"); } - auto lib_context = efl::eolian::grammar::context_add_tag(eolian_mono::library_context{opts.dllimport, - opts.v_major, - opts.v_minor, - opts.references_map}, - efl::eolian::grammar::context_null()); - - auto options_context = efl::eolian::grammar::context_add_tag(eolian_mono::options_context{opts.want_beta}, lib_context); - - auto context = efl::eolian::grammar::context_add_tag(eolian_mono::eolian_state_context{opts.state}, options_context); + using efl::eolian::grammar::context_add_tag; + + auto context = context_add_tag(eolian_mono::indentation_context{0}, + context_add_tag(eolian_mono::eolian_state_context{opts.state}, + context_add_tag(eolian_mono::options_context{opts.want_beta}, + context_add_tag(eolian_mono::library_context{opts.dllimport, + opts.v_major, + opts.v_minor, + opts.references_map}, + efl::eolian::grammar::context_null())))); EINA_ITERATOR_FOREACH(aliases, tp) { -- 2.7.4