Commit d57e3129 authored by Xavier Thompson's avatar Xavier Thompson

Clarify handling of inherited methods hidden by a subclass method

parent c72daa17
......@@ -509,11 +509,9 @@ class Scope(object):
entries = self.entries
# The index of an inherited c method among the overloaded alternatives in a cpp class
old_index = -1
# The index of the predeclared default constructor among the alternative constructors in a cypclass
cpp_no_args_constructor_index = -1
# The index of an overridable overloaded alternative that this declaration will hide.
# Stays -1 if there isn't one.
previous_alternative_index = -1
if name and name in entries and not shadow:
old_entry = entries[name]
......@@ -523,17 +521,18 @@ class Scope(object):
for index, alt_entry in enumerate(old_entry.all_alternatives()):
if type.compatible_signature_with(alt_entry.type):
# If we're not in a cypclass, keep the same behaviour as before
# If we're not in a cypclass, any inherited method is visible
# until overloaded by a method with the same siganture
if not (self.type and self.type.is_cyp_class):
if alt_entry.is_inherited:
old_index = index
previous_alternative_index = index
# In a cypclass, only the predeclared default constructor is allowed to be redeclared.
# We don't have to deal with inherited entries because they are hidden as soon as the subclass has a method
# of the same name, regardless of the exact signature (this is also C++ behavior by default)
elif name == '<constructor>' and (not type.args or len(type.args) == type.optional_arg_count):
# Cython pre-declares the no-args constructor - allow later user definitions.
cpp_no_args_constructor_index = index
previous_alternative_index = index
cpp_override_allowed = True
break
......@@ -562,27 +561,18 @@ class Scope(object):
if not shadow:
if name in entries and self.is_cpp_class_scope and type.is_cfunction:
# If we're not in a cypclass, keep the same behaviour as before
if not (self.type and self.type.is_cyp_class):
if old_index > -1:
if old_index > 0:
entries[name].overloaded_alternatives[old_index-1] = entry
else:
entry.overloaded_alternatives = entries[name].overloaded_alternatives
entries[name] = entry
else:
if previous_alternative_index == -1:
# if no previous alternative is hidden by this one, just add it to the list
entries[name].overloaded_alternatives.append(entry)
# In a cypclass, only the predeclared default constructor might need to be replaced
elif previous_alternative_index > 0:
# if there is a now hidden previous alternative, replace it
entries[name].overloaded_alternatives[previous_alternative_index-1] = entry
else:
if cpp_no_args_constructor_index == 0:
# if the first previous alternative is now hidden, replace it
entry.overloaded_alternatives = entries[name].overloaded_alternatives
entries[name] = entry
elif cpp_no_args_constructor_index > 0:
entries[name].overloaded_alternatives[cpp_no_args_constructor_index-1] = entry
else:
entries[name].overloaded_alternatives.append(entry)
else:
entries[name] = entry
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment