Make `Options::assign()` specializations free functions (#6648)
authorGereon Kremer <nafur42@gmail.com>
Wed, 2 Jun 2021 06:55:24 +0000 (08:55 +0200)
committerGitHub <noreply@github.com>
Wed, 2 Jun 2021 06:55:24 +0000 (06:55 +0000)
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.

src/options/mkoptions.py
src/options/options_template.cpp
src/options/options_template.h
src/options/printer_modes.cpp
src/options/printer_modes.h

index fc52dfe0d8a4734772bb8b4ff9a0186e1dd12eff..91a5c32e0497e1e0147d5fa9aa1faf95b57b885d 100644 (file)
@@ -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,
index 091acfd7a68cc744fb6b96f521c2338388dd7e05..251072ba5371fe4fbb7dc9dadbf444943c32ce3d 100644 (file)
@@ -416,7 +416,7 @@ void Options::parseOptionsRecursive(int argc,
                                     char* argv[],
                                     std::vector<std::string>* 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);
 }
index 502dfb833901cb3e10983fac66bf5bc65a0ba05b..346096169ff5ead9fac328fc7729f72bb4829486 100644 (file)
@@ -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 <class T>
-  void assign(T, std::string option, std::string value);
-  /** Low-level assignment function for bool-valued options */
-  template <class T>
-  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
index fc765db017f6e7bf3b10461e284ed7fc1c271921..b2d8ec79ae658c6e7249c6a1a26b522ee5c84bec 100644 (file)
@@ -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)
index e27cb133f6c3b4fd70abfdcf25f13d6fa6f12cf0..093bff7d99ce489ad0bf1b95a5711a5cfc136e42 100644 (file)
@@ -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