Commit 668b616b authored by Xavier Thompson's avatar Xavier Thompson

Use dynamic_cast when downcasting from a base to a cypclass

parent 0faa42cc
......@@ -10862,8 +10862,13 @@ class TypecastNode(ExprNode):
real_part,
imag_part)
else:
if self.overloaded and self.operand.type.is_cyp_class:
operand_result = '(*%s)' % operand_result
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):
......@@ -10889,11 +10894,19 @@ class TypecastNode(ExprNode):
elif self.type.is_cyp_class:
star = "*" if self.overloaded else ""
operand_result = "%s%s" % (star, self.operand.result())
code.putln(
"%s = (%s)(%s);" % (
self.result(),
self.type.declaration_code(''),
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(),
self.type.declaration_code(''),
operand_result))
code.put_cyincref(self.result())
......
......@@ -4153,6 +4153,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