From cdf6cd6e3b2cd97f4bbca9d3f48c3eba83fc9fd1 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Tue, 26 Nov 2013 09:20:44 -0500 Subject: [PATCH] nitg: introduce A`CV::add_cast` to factorize cast checks Signed-off-by: Jean Privat --- src/abstract_compiler.nit | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/abstract_compiler.nit b/src/abstract_compiler.nit index 529f3b9..cdd1535 100644 --- a/src/abstract_compiler.nit +++ b/src/abstract_compiler.nit @@ -908,6 +908,15 @@ abstract class AbstractCompilerVisitor self.add("exit(1);") end + # Add a dynamic cast + fun add_cast(value: RuntimeVariable, mtype: MType, tag: String) + do + var res = self.type_test(value, mtype, tag) + self.add("if (!{res}) \{") + self.add_abort("Cast failed") + self.add("\}") + end + # Generate a return with the value `s` fun ret(s: RuntimeVariable) do @@ -939,10 +948,7 @@ abstract class AbstractCompilerVisitor res = autoadapt(res, nexpr.mtype.as(not null)) var implicit_cast_to = nexpr.implicit_cast_to if implicit_cast_to != null and not self.compiler.modelbuilder.toolcontext.opt_no_check_autocast.value then - var castres = self.type_test(res, implicit_cast_to, "auto") - self.add("if (!{castres}) \{") - self.add_abort("Cast failed") - self.add("\}") + add_cast(res, implicit_cast_to, "auto") res = autoadapt(res, implicit_cast_to) end self.current_node = old @@ -1290,10 +1296,7 @@ redef class MMethodDef # generate the cast # note that v decides if and how to implements the cast v.add("/* Covariant cast for argument {i} ({self.msignature.mparameters[i].name}) {arguments[i+1].inspect} isa {mtype} */") - var cond = v.type_test(arguments[i+1], mtype, "covariance") - v.add("if (!{cond}) \{") - v.add_abort("Cast failed") - v.add("\}") + v.add_cast(arguments[i+1], mtype, "covariance") end end end @@ -2179,10 +2182,7 @@ redef class AAsCastExpr var i = v.expr(self.n_expr, null) if v.compiler.modelbuilder.toolcontext.opt_no_check_assert.value then return i - var cond = v.type_test(i, self.mtype.as(not null), "as") - v.add("if (!{cond}) \{") - v.add_abort("Cast failed") - v.add("\}") + v.add_cast(i, self.mtype.as(not null), "as") return i end end -- 1.7.9.5