Commit 713a1085 authored by Xavier Thompson's avatar Xavier Thompson

Use dynamic_cast when downcasting from a base to a cypclass

parent c02020b0
...@@ -10765,8 +10765,13 @@ class TypecastNode(ExprNode): ...@@ -10765,8 +10765,13 @@ class TypecastNode(ExprNode):
real_part, real_part,
imag_part) imag_part)
else: else:
if self.overloaded and self.operand.type.is_cyp_class: operand_type = self.operand.type
operand_result = '(*%s)' % operand_result 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) return self.type.cast_code(operand_result)
def get_constant_c_result_code(self): def get_constant_c_result_code(self):
...@@ -10792,11 +10797,19 @@ class TypecastNode(ExprNode): ...@@ -10792,11 +10797,19 @@ class TypecastNode(ExprNode):
elif self.type.is_cyp_class: elif self.type.is_cyp_class:
star = "*" if self.overloaded else "" star = "*" if self.overloaded else ""
operand_result = "%s%s" % (star, self.operand.result()) operand_result = "%s%s" % (star, self.operand.result())
code.putln( # use dynamic cast when dowcasting from a base to a cypclass
"%s = (%s)(%s);" % ( if self.operand.type in self.type.mro():
self.result(), code.putln(
self.type.declaration_code(''), "%s = dynamic_cast<%s>(%s);" % (
operand_result)) 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()) code.put_cyincref(self.result())
......
...@@ -4008,6 +4008,9 @@ class CypClassType(CppClassType): ...@@ -4008,6 +4008,9 @@ class CypClassType(CppClassType):
def cast_code(self, expr_code): def cast_code(self, expr_code):
return "((%s)%s)" % (self.declaration_code(''), 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): 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: 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 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