From: Gereon Kremer Date: Wed, 2 Jun 2021 06:55:24 +0000 (+0200) Subject: Make `Options::assign()` specializations free functions (#6648) X-Git-Tag: cvc5-1.0.0~1663 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=4dacf3183d6790ebb4615263908da294e43e3cb6;p=cvc5.git Make `Options::assign()` specializations free functions (#6648) This PR removes the next two heavily specialized template functions. Both Options::assign() and Options::assignBool() are only used within options.cpp now and there is thus no reason to keep them in the public interface. Furthermore, we can just make them properly named functions instead of template functions. --- diff --git a/src/options/mkoptions.py b/src/options/mkoptions.py index fc52dfe0d..91a5c32e0 100644 --- a/src/options/mkoptions.py +++ b/src/options/mkoptions.py @@ -80,35 +80,25 @@ g_getopt_long_start = 256 ### Source code templates -TPL_IMPL_ASSIGN = \ -"""template <> void Options::assign( - options::{name}__option_t, - std::string option, - std::string optionarg) -{{ - auto parsedval = {handler}; +TPL_ASSIGN = ''' +void assign_{module}_{name}(Options& opts, const std::string& option, const std::string& optionarg) {{ + auto value = {handler}; {predicates} - {module}.{name} = parsedval; - {module}.{name}__setByUser = true; - Trace("options") << "user assigned option {name}" << std::endl; -}}""" + opts.{module}.{name} = value; + opts.{module}.{name}__setByUser = true; + Trace("options") << "user assigned option {name} = " << value << std::endl; +}}''' -TPL_IMPL_ASSIGN_BOOL = \ -"""template <> void Options::assignBool( - options::{name}__option_t, - std::string option, - bool value) -{{ +TPL_ASSIGN_BOOL = ''' +void assign_{module}_{name}(Options& opts, const std::string& option, bool value) {{ {predicates} - {module}.{name} = value; - {module}.{name}__setByUser = true; - Trace("options") << "user assigned option {name}" << std::endl; -}}""" - -TPL_CALL_ASSIGN_BOOL = \ - ' assignBool(options::{name}, {option}, {value});' + opts.{module}.{name} = value; + opts.{module}.{name}__setByUser = true; + Trace("options") << "user assigned option {name} = " << value << std::endl; +}}''' -TPL_CALL_ASSIGN = ' assign(options::{name}, {option}, optionarg);' +TPL_CALL_ASSIGN_BOOL = ' assign_{module}_{name}(opts, {option}, {value});' +TPL_CALL_ASSIGN = ' assign_{module}_{name}(opts, {option}, optionarg);' TPL_CALL_SET_OPTION = 'setOption(std::string("{smtname}"), ("{value}"));' @@ -770,10 +760,10 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_ handler = None if option.handler: if option.type == 'void': - handler = 'd_handler->{}(option)'.format(option.handler) + handler = 'opts.handler().{}(option)'.format(option.handler) else: handler = \ - 'd_handler->{}(option, optionarg)'.format(option.handler) + 'opts.handler().{}(option, optionarg)'.format(option.handler) elif option.mode: handler = 'stringTo{}(optionarg)'.format(option.type) elif option.type != 'bool': @@ -785,12 +775,12 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_ if option.predicates: if option.type == 'bool': predicates = \ - ['d_handler->{}(option, value);'.format(x) \ + ['opts.handler().{}(option, value);'.format(x) \ for x in option.predicates] else: assert option.type != 'void' predicates = \ - ['d_handler->{}(option, parsedval);'.format(x) \ + ['opts.handler().{}(option, value);'.format(x) \ for x in option.predicates] # Generate options_handler and getopt_long @@ -820,12 +810,14 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_ if option.type == 'bool' and option.name: cases.append( TPL_CALL_ASSIGN_BOOL.format( + module=module.id, name=option.name, option='option', value='true')) elif option.type != 'void' and option.name: cases.append( TPL_CALL_ASSIGN.format( + module=module.id, name=option.name, option='option', value='optionarg')) @@ -854,12 +846,14 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_ if option.type == 'bool': setoption_handlers.append( TPL_CALL_ASSIGN_BOOL.format( + module=module.id, name=option.name, option='key', value='optionarg == "true"')) elif argument_req and option.name: setoption_handlers.append( TPL_CALL_ASSIGN.format( + module=module.id, name=option.name, option='key')) elif option.handler: @@ -914,9 +908,9 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_ cases.append( TPL_CALL_ASSIGN_BOOL.format( + module=module.id, name=option.name, option='option', value='false')) cases.append(' break;') - options_handler.extend(cases) optname = option.long @@ -943,13 +937,15 @@ def codegen_all_modules(modules, build_dir, dst_dir, tpl_options_h, tpl_options_ # Define handler assign/assignBool - tpl = None if option.type == 'bool': - tpl = TPL_IMPL_ASSIGN_BOOL + custom_handlers.append(TPL_ASSIGN_BOOL.format( + module=module.id, + name=option.name, + handler=handler, + predicates='\n'.join(predicates) + )) elif option.short or option.long: - tpl = TPL_IMPL_ASSIGN - if tpl: - custom_handlers.append(tpl.format( + custom_handlers.append(TPL_ASSIGN.format( module=module.id, name=option.name, handler=handler, diff --git a/src/options/options_template.cpp b/src/options/options_template.cpp index 091acfd7a..251072ba5 100644 --- a/src/options/options_template.cpp +++ b/src/options/options_template.cpp @@ -416,7 +416,7 @@ void Options::parseOptionsRecursive(int argc, char* argv[], std::vector* nonoptions) { - + Options& opts = *this; if(Debug.isOn("options")) { Debug("options") << "starting a new parseOptionsRecursive with " << argc << " arguments" << std::endl; @@ -553,6 +553,7 @@ void Options::setOptionInternal(const std::string& key, const std::string& optionarg) { options::OptionsHandler* handler = d_handler; + Options& opts = *this; ${setoption_handlers}$ throw UnrecognizedOptionException(key); } diff --git a/src/options/options_template.h b/src/options/options_template.h index 502dfb833..346096169 100644 --- a/src/options/options_template.h +++ b/src/options/options_template.h @@ -63,13 +63,6 @@ ${holder_ref_decls}$ /** The current Options in effect */ static thread_local Options* s_current; - /** Low-level assignment function for options */ - template - void assign(T, std::string option, std::string value); - /** Low-level assignment function for bool-valued options */ - template - void assignBool(T, std::string option, bool value); - friend class options::OptionsHandler; /** @@ -113,6 +106,9 @@ public: Options(OptionsListener* ol = nullptr); ~Options(); + options::OptionsHandler& handler() const { + return *d_handler; + } /** * Copies the value of the options stored in OptionsHolder into the current diff --git a/src/options/printer_modes.cpp b/src/options/printer_modes.cpp index fc765db01..b2d8ec79a 100644 --- a/src/options/printer_modes.cpp +++ b/src/options/printer_modes.cpp @@ -18,9 +18,9 @@ #include "options/printer_modes.h" -namespace cvc5 { +namespace cvc5::options { -std::ostream& operator<<(std::ostream& out, options::InstFormatMode mode) +std::ostream& operator<<(std::ostream& out, InstFormatMode mode) { out << "InstFormatMode::"; switch (mode) diff --git a/src/options/printer_modes.h b/src/options/printer_modes.h index e27cb133f..093bff7d9 100644 --- a/src/options/printer_modes.h +++ b/src/options/printer_modes.h @@ -36,9 +36,9 @@ enum class InstFormatMode SZS, }; -} // namespace options +std::ostream& operator<<(std::ostream& out, InstFormatMode mode); -std::ostream& operator<<(std::ostream& out, options::InstFormatMode mode); +} // namespace options } // namespace cvc5