Commit 5bc69aa8 authored by Xavier Thompson's avatar Xavier Thompson

Use dynamic_cast when downcasting from a base to a cypclass

parent f49fc8fe
......@@ -10905,8 +10905,13 @@ class TypecastNode(ExprNode):
real_part,
imag_part)
else:
if self.overloaded and self.operand.type.is_cyp_class:
operand_type = self.operand.type
if operand_type.is_cyp_class:
if self.overloaded:
operand_result = '(*%s)' % operand_result
# use dynamic cast when dowcasting from a base to a cypclass
if self.type.is_cyp_class and operand_type in self.type.mro():
return self.type.dynamic_cast_code(operand_result)
return self.type.cast_code(operand_result)
def get_constant_c_result_code(self):
......@@ -10932,6 +10937,14 @@ class TypecastNode(ExprNode):
elif self.type.is_cyp_class:
star = "*" if self.overloaded else ""
operand_result = "%s%s" % (star, self.operand.result())
# use dynamic cast when dowcasting from a base to a cypclass
if self.operand.type in self.type.mro():
code.putln(
"%s = dynamic_cast<%s>(%s);" % (
self.result(),
self.type.declaration_code(''),
operand_result))
else:
code.putln(
"%s = (%s)(%s);" % (
self.result(),
......
......@@ -4223,6 +4223,9 @@ class CypClassType(CppClassType):
def cast_code(self, expr_code):
return "((%s)%s)" % (self.declaration_code(''), expr_code)
def dynamic_cast_code(self, expr_code):
return "dynamic_cast<%s>(%s)" % (self.declaration_code(''), expr_code)
def assignable_from_resolved_type(self, other_type):
if other_type.is_ptr and other_type.base_type.is_cpp_class and other_type.base_type.is_subclass(self) or other_type.is_null_ptr:
return 1
......
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