From: Jean Privat Date: Fri, 25 Mar 2016 00:10:59 +0000 (-0400) Subject: nitc/abscomp: add helper function maybenull to factorize code X-Git-Url: http://nitlanguage.org nitc/abscomp: add helper function maybenull to factorize code Signed-off-by: Jean Privat --- diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index 1fad40d..47a0017 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -1351,13 +1351,18 @@ abstract class AbstractCompilerVisitor # Checks + # Can value be null? (according to current knowledge) + fun maybenull(value: RuntimeVariable): Bool + do + return value.mcasttype isa MNullableType or value.mcasttype isa MNullType + end + # Add a check and an abort for a null receiver if needed fun check_recv_notnull(recv: RuntimeVariable) do if self.compiler.modelbuilder.toolcontext.opt_no_check_null.value then return - var maybenull = recv.mcasttype isa MNullableType or recv.mcasttype isa MNullType - if maybenull then + if maybenull(recv) then self.add("if (unlikely({recv} == NULL)) \{") self.add_abort("Receiver is null") self.add("\}") diff --git a/src/compiler/separate_compiler.nit b/src/compiler/separate_compiler.nit index 267eae0..362bb52 100644 --- a/src/compiler/separate_compiler.nit +++ b/src/compiler/separate_compiler.nit @@ -1399,8 +1399,7 @@ class SeparateCompilerVisitor var res: nullable RuntimeVariable = null var recv = arguments.first var consider_null = not self.compiler.modelbuilder.toolcontext.opt_no_check_null.value or mmethod.name == "==" or mmethod.name == "!=" - var maybenull = (recv.mcasttype isa MNullableType or recv.mcasttype isa MNullType) and consider_null - if maybenull then + if maybenull(recv) and consider_null then self.add("if ({recv} == NULL) \{") if mmethod.name == "==" or mmethod.name == "is_same_instance" then res = self.new_var(bool_type)