From 8a205ac100ec4610c13fc7e7dbabf9e4f236a072 Mon Sep 17 00:00:00 2001 From: rdb Date: Mon, 25 Jan 2016 17:02:25 +0100 Subject: [PATCH] MAKE_PROPERTY should ignore setter overloads that take the wrong number of arguments --- dtool/src/interrogate/functionRemap.cxx | 41 ++++++++++++ dtool/src/interrogate/functionRemap.h | 3 + .../interfaceMakerPythonNative.cxx | 66 ++++++------------- 3 files changed, 63 insertions(+), 47 deletions(-) diff --git a/dtool/src/interrogate/functionRemap.cxx b/dtool/src/interrogate/functionRemap.cxx index f074a954a4..eaeb70fb78 100644 --- a/dtool/src/interrogate/functionRemap.cxx +++ b/dtool/src/interrogate/functionRemap.cxx @@ -504,6 +504,47 @@ get_call_str(const string &container, const vector_string &pexprs) const { return call.str(); } +//////////////////////////////////////////////////////////////////// +// Function: FunctionRemap::get_min_num_args +// Access: Private +// Description: Returns the minimum number of arguments that needs +// to be passed to this function. +//////////////////////////////////////////////////////////////////// +int FunctionRemap:: +get_min_num_args() const { + int min_num_args = 0; + Parameters::const_iterator pi; + pi = _parameters.begin(); + if (_has_this && pi != _parameters.end()) { + ++pi; + } + for (; pi != _parameters.end(); ++pi) { + ParameterRemap *param = (*pi)._remap; + if (param->get_default_value() != (CPPExpression *)NULL) { + // We've reached the first parameter that takes a default value. + break; + } else { + ++min_num_args; + } + } + return min_num_args; +} + +//////////////////////////////////////////////////////////////////// +// Function: FunctionRemap::get_max_num_args +// Access: Private +// Description: Returns the maximum number of arguments that can +// be passed to this function. +//////////////////////////////////////////////////////////////////// +int FunctionRemap:: +get_max_num_args() const { + int max_num_args = _parameters.size(); + if (_has_this && _type != FunctionRemap::T_constructor) { + --max_num_args; + } + return max_num_args; +} + //////////////////////////////////////////////////////////////////// // Function: FunctionRemap::get_parameter_expr // Access: Private diff --git a/dtool/src/interrogate/functionRemap.h b/dtool/src/interrogate/functionRemap.h index dadd26c45d..aa63dfb905 100644 --- a/dtool/src/interrogate/functionRemap.h +++ b/dtool/src/interrogate/functionRemap.h @@ -65,6 +65,9 @@ public: string get_call_str(const string &container, const vector_string &pexprs) const; + int get_min_num_args() const; + int get_max_num_args() const; + class Parameter { public: bool _has_name; diff --git a/dtool/src/interrogate/interfaceMakerPythonNative.cxx b/dtool/src/interrogate/interfaceMakerPythonNative.cxx index 7b91c8dd25..18f96deec8 100644 --- a/dtool/src/interrogate/interfaceMakerPythonNative.cxx +++ b/dtool/src/interrogate/interfaceMakerPythonNative.cxx @@ -3371,35 +3371,16 @@ write_function_for_name(ostream &out, Object *obj, for (ri = remaps.begin(); ri != remaps.end(); ++ri) { remap = (*ri); if (is_remap_legal(remap)) { - int max_num_args = remap->_parameters.size(); + int min_num_args = remap->get_min_num_args(); + int max_num_args = remap->get_max_num_args(); if (remap->_has_this) { has_this = true; - - if (remap->_type != FunctionRemap::T_constructor) { - max_num_args--; - } } if (!remap->_has_this || remap->_const_method) { all_nonconst = false; } - int min_num_args = 0; - FunctionRemap::Parameters::const_iterator pi; - pi = remap->_parameters.begin(); - if (remap->_has_this && pi != remap->_parameters.end()) { - ++pi; - } - for (; pi != remap->_parameters.end(); ++pi) { - ParameterRemap *param = (*pi)._remap; - if (param->get_default_value() != (CPPExpression *)NULL) { - // We've reached the first parameter that takes a default value. - break; - } else { - ++min_num_args; - } - } - max_required_args = max(max_num_args, max_required_args); for (int i = min_num_args; i <= max_num_args; ++i) { @@ -3694,18 +3675,8 @@ write_coerce_constructor(ostream &out, Object *obj, bool is_const) { continue; } - int max_num_args = remap->_parameters.size(); - int min_num_args = 0; - FunctionRemap::Parameters::const_iterator pi; - for (pi = remap->_parameters.begin(); pi != remap->_parameters.end(); ++pi) { - ParameterRemap *param = (*pi)._remap; - if (param->get_default_value() != (CPPExpression *)NULL) { - // We've reached the first parameter that takes a default value. - break; - } else { - ++min_num_args; - } - } + int min_num_args = remap->get_min_num_args(); + int max_num_args = remap->get_max_num_args(); // Coerce constructor should take at least one argument. nassertd(max_num_args > 0) continue; @@ -3732,18 +3703,8 @@ write_coerce_constructor(ostream &out, Object *obj, bool is_const) { if (is_remap_legal(remap) && remap->_flags & FunctionRemap::F_coerce_constructor) { nassertd(!remap->_has_this) continue; - int max_num_args = remap->_parameters.size(); - int min_num_args = 0; - FunctionRemap::Parameters::const_iterator pi; - for (pi = remap->_parameters.begin(); pi != remap->_parameters.end(); ++pi) { - ParameterRemap *param = (*pi)._remap; - if (param->get_default_value() != (CPPExpression *)NULL) { - // We've reached the first parameter that takes a default value. - break; - } else { - ++min_num_args; - } - } + int min_num_args = remap->get_min_num_args(); + int max_num_args = remap->get_max_num_args(); // Coerce constructor should take at least one argument. nassertd(max_num_args > 0) continue; @@ -6495,8 +6456,19 @@ write_getset(ostream &out, Object *obj, Property *property) { } std::set remaps; - remaps.insert(property->_setter->_remaps.begin(), - property->_setter->_remaps.end()); + + // Extract only the setters that take one argument. + Function::Remaps::iterator it; + for (it = property->_setter->_remaps.begin(); + it != property->_setter->_remaps.end(); + ++it) { + FunctionRemap *remap = *it; + int min_num_args = remap->get_min_num_args(); + int max_num_args = remap->get_max_num_args(); + if (min_num_args <= 1 && max_num_args >= 1) { + remaps.insert(remap); + } + } string expected_params; write_function_forset(out, remaps, 1, 1,