nullable: type, compile and test 'as(not null)'
[nit.git] / src / compiling / compiling_methods.nit
index a8bbd6d..935b3d0 100644 (file)
@@ -629,6 +629,14 @@ redef class MMType
                var g = local_class.global
                v.add_instr("if (({recv}!=NIT_NULL) && !VAL_ISA({recv}, {g.color_id}, {g.id_id})) \{ fprintf(stderr, \"Cast failled\"); {v.printf_locate_error(n)} nit_exit(1); } /*cast {self}*/;")
        end
+
+       # Compile a notnull cast assertion
+       meth compile_notnull_check(v: CompilerVisitor, recv: String, n: PNode)
+       do
+               if is_nullable then
+                       v.add_instr("if (({recv}==NIT_NULL)) \{ fprintf(stderr, \"Cast failled\"); {v.printf_locate_error(n)} nit_exit(1); } /*cast {self}*/;")
+               end
+       end
 end
 
 ###############################################################################
@@ -1230,6 +1238,15 @@ redef class AAsCastExpr
        end
 end
 
+redef class AAsNotnullExpr
+       redef meth compile_expr(v)
+       do
+               var e = v.compile_expr(n_expr)
+               n_expr.stype.compile_notnull_check(v, e, self)
+               return e
+       end
+end
+
 redef class ATrueExpr
        redef meth compile_expr(v)
        do