nitg: implements implicit casts for unsafe nexpr
authorJean Privat <jean@pryen.org>
Fri, 7 Dec 2012 20:25:38 +0000 (15:25 -0500)
committerJean Privat <jean@pryen.org>
Fri, 7 Dec 2012 21:11:28 +0000 (16:11 -0500)
add the option --no-check-autocast to disable it.

Signed-off-by: Jean Privat <jean@pryen.org>

src/global_compiler.nit
src/rapid_type_analysis.nit
src/run_bench.sh
tests/sav/nitg.sav

index 66969f7..2f63d20 100644 (file)
@@ -47,6 +47,9 @@ redef class ToolContext
        # --no-check-assert
        var opt_no_check_assert: OptionBool = new OptionBool("Disable the evaluation of explicit 'assert' and 'as' (dangerous)", "--no-check-assert")
 
+       # --no-check-autocast
+       var opt_no_check_autocast: OptionBool = new OptionBool("Disable implicit casts on unsafe expression usage (dangerous)", "--no-check-autocast")
+
        # --no-check-other
        var opt_no_check_other: OptionBool = new OptionBool("Disable implicit tests: unset attribute, null receiver (dangerous)", "--no-check-other")
 
@@ -54,7 +57,7 @@ redef class ToolContext
        do
                super
                self.option_context.add_option(self.opt_output, self.opt_no_cc, self.opt_hardening)
-               self.option_context.add_option(self.opt_no_check_covariance, self.opt_no_check_initialization, self.opt_no_check_assert, self.opt_no_check_other)
+               self.option_context.add_option(self.opt_no_check_covariance, self.opt_no_check_initialization, self.opt_no_check_assert, self.opt_no_check_autocast, self.opt_no_check_other)
        end
 end
 
@@ -951,6 +954,13 @@ class GlobalCompilerVisitor
                        mtype = self.anchor(mtype)
                        res = self.autobox(res, mtype)
                end
+               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)
+                       self.add("if (!{castres}) \{")
+                       self.add_abort("Cast failed")
+                       self.add("\}")
+               end
                self.current_node = old
                return res
        end
index 2a89d11..fdea6f9 100644 (file)
@@ -398,6 +398,10 @@ private class RapidTypeVisitor
        do
                if node == null then return
                node.accept_rapid_type_vistor(self)
+               if node isa AExpr then
+                       var implicit_cast_to = node.implicit_cast_to
+                       if implicit_cast_to != null then self.add_cast_type(implicit_cast_to)
+               end
                node.visit_all(self)
        end
 
index 369cdcf..c44de47 100755 (executable)
@@ -306,7 +306,7 @@ function bench_nitg_options()
 
        plot "$name.gnu"
 }
-bench_nitg_options --hardening --no-check-covariance --no-check-initialization --no-check-assert --no-check-other
+bench_nitg_options --hardening --no-check-covariance --no-check-initialization --no-check-assert --no-check-autocast --no-check-other
 
 function bench_nitg-e_options()
 {
index a46fad5..2d1fbc2 100644 (file)
@@ -16,6 +16,7 @@
   --no-check-covariance       Disable type tests of covariant parameters (dangerous)
   --no-check-initialization   Disable isset tests at the end of constructors (dangerous)
   --no-check-assert           Disable the evaluation of explicit 'assert' and 'as' (dangerous)
+  --no-check-autocast         Disable implicit casts on unsafe expression usage (dangerous)
   --no-check-other            Disable implicit tests: unset attribute, null receiver (dangerous)
   --separate                  Use separate compilation
   --no-inline-intern          Do not inline call to intern methods