Commit 2da25603 authored by Robert Bradshaw's avatar Robert Bradshaw

Allow constant typecasts.

parent 10e71628
...@@ -7341,6 +7341,9 @@ class TypecastNode(ExprNode): ...@@ -7341,6 +7341,9 @@ class TypecastNode(ExprNode):
if self.type is None: if self.type is None:
base_type = self.base_type.analyse(env) base_type = self.base_type.analyse(env)
_, self.type = self.declarator.analyse(base_type, env) _, self.type = self.declarator.analyse(base_type, env)
if self.operand.has_constant_result():
# Must be done after self.type is resolved.
self.calculate_constant_result()
if self.type.is_cfunction: if self.type.is_cfunction:
error(self.pos, error(self.pos,
"Cannot cast to a function type") "Cannot cast to a function type")
...@@ -7400,11 +7403,11 @@ class TypecastNode(ExprNode): ...@@ -7400,11 +7403,11 @@ class TypecastNode(ExprNode):
return self.operand.check_const() return self.operand.check_const()
def calculate_constant_result(self): def calculate_constant_result(self):
# we usually do not know the result of a type cast at code self.constant_result = self.calculate_result_code(self.operand.constant_result)
# generation time
pass
def calculate_result_code(self): def calculate_result_code(self, operand_result = None):
if operand_result is None:
operand_result = self.operand.result()
if self.type.is_complex: if self.type.is_complex:
operand_result = self.operand.result() operand_result = self.operand.result()
if self.operand.type.is_complex: if self.operand.type.is_complex:
...@@ -7418,7 +7421,7 @@ class TypecastNode(ExprNode): ...@@ -7418,7 +7421,7 @@ class TypecastNode(ExprNode):
real_part, real_part,
imag_part) imag_part)
else: else:
return self.type.cast_code(self.operand.result()) return self.type.cast_code(operand_result)
def get_constant_c_result_code(self): def get_constant_c_result_code(self):
operand_result = self.operand.get_constant_c_result_code() operand_result = self.operand.get_constant_c_result_code()
......
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