From 1664782aa66fcb01f5f00662759f978605d76d68 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Thu, 29 Nov 2012 09:08:10 -0500 Subject: [PATCH] tests: nitg can compile empty modules create a new method compile_main_function to factorize the work Signed-off-by: Jean Privat --- src/global_compiler.nit | 57 ++++++++++++-------- src/separate_compiler.nit | 23 ++------ tests/base_types_formal_and_virtual3.nit | 3 ++ tests/sav/nitg/fixme/base_attr_init_val2.res | 1 - tests/sav/nitg/fixme/base_empty_module.res | 1 - .../nitg/fixme/base_types_formal_and_virtual.res | 1 - .../nitg/fixme/base_types_formal_and_virtual2.res | 1 - .../nitg/fixme/base_types_formal_and_virtual3.res | 1 - .../nitg/fixme/base_types_formal_and_virtual4.res | 1 - tests/sav/nitg/fixme/error_needed_types.res | 1 - 10 files changed, 42 insertions(+), 48 deletions(-) delete mode 100644 tests/sav/nitg/fixme/base_attr_init_val2.res delete mode 100644 tests/sav/nitg/fixme/base_empty_module.res delete mode 100644 tests/sav/nitg/fixme/base_types_formal_and_virtual.res delete mode 100644 tests/sav/nitg/fixme/base_types_formal_and_virtual2.res delete mode 100644 tests/sav/nitg/fixme/base_types_formal_and_virtual3.res delete mode 100644 tests/sav/nitg/fixme/base_types_formal_and_virtual4.res delete mode 100644 tests/sav/nitg/fixme/error_needed_types.res diff --git a/src/global_compiler.nit b/src/global_compiler.nit index a67bd61..610d5fc 100644 --- a/src/global_compiler.nit +++ b/src/global_compiler.nit @@ -102,27 +102,7 @@ redef class ModelBuilder end # The main function of the C - - v = new GlobalCompilerVisitor(compiler) - v.add_decl("int glob_argc;") - v.add_decl("char **glob_argv;") - v.add_decl("val *glob_sys;") - v.add_decl("int main(int argc, char** argv) \{") - v.add("glob_argc = argc; glob_argv = argv;") - var main_type = mainmodule.sys_type - if main_type == null then return # Nothing to compile - var glob_sys = v.init_instance(main_type) - v.add("glob_sys = {glob_sys};") - var main_init = mainmodule.try_get_primitive_method("init", main_type) - if main_init != null then - v.send(main_init, [glob_sys]) - end - var main_method = mainmodule.try_get_primitive_method("main", main_type) - if main_method != null then - v.send(main_method, [glob_sys]) - end - v.add("return 0;") - v.add("\}") + compiler.compile_main_function # Compile until all runtime_functions are visited @@ -439,6 +419,41 @@ class GlobalCompiler end end + # Initialize a visitor specific for a compiler engine + fun new_visitor: GlobalCompilerVisitor do return new GlobalCompilerVisitor(self) + + # Generate the main C function. + # This function: + # allocate the Sys object if it exists + # call init if is exists + # call main if it exists + fun compile_main_function + do + var v = self.new_visitor + v.add_decl("int glob_argc;") + v.add_decl("char **glob_argv;") + v.add_decl("val *glob_sys;") + v.add_decl("int main(int argc, char** argv) \{") + v.add("glob_argc = argc; glob_argv = argv;") + var main_type = mainmodule.sys_type + if main_type != null then + var mainmodule = v.compiler.mainmodule + var glob_sys = v.init_instance(main_type) + v.add("glob_sys = {glob_sys};") + var main_init = mainmodule.try_get_primitive_method("init", main_type) + if main_init != null then + v.send(main_init, [glob_sys]) + end + var main_method = mainmodule.try_get_primitive_method("main", main_type) + if main_method != null then + v.send(main_method, [glob_sys]) + end + end + v.add("return 0;") + v.add("\}") + + end + private var collect_types_cache: HashMap[MType, Array[MClassType]] = new HashMap[MType, Array[MClassType]] end diff --git a/src/separate_compiler.nit b/src/separate_compiler.nit index b63c1f3..b533884 100644 --- a/src/separate_compiler.nit +++ b/src/separate_compiler.nit @@ -69,26 +69,7 @@ redef class ModelBuilder v.add_decl("extern val *glob_sys;") # The main function of the C - - v = new SeparateCompilerVisitor(compiler) - v.add_decl("int glob_argc;") - v.add_decl("char **glob_argv;") - v.add_decl("val *glob_sys;") - v.add_decl("int main(int argc, char** argv) \{") - v.add("glob_argc = argc; glob_argv = argv;") - var main_type = mainmodule.sys_type - if main_type == null then return # Nothing to compile - var glob_sys = v.init_instance(main_type) - v.add("glob_sys = {glob_sys};") - var main_init = mainmodule.try_get_primitive_method("init", main_type) - if main_init != null then - v.send(main_init, [glob_sys]) - end - var main_method = mainmodule.try_get_primitive_method("main", main_type) - if main_method != null then - v.send(main_method, [glob_sys]) - end - v.add("\}") + compiler.compile_main_function # compile class structures for m in mainmodule.in_importation.greaters do @@ -542,6 +523,8 @@ class SeparateCompiler v.add("return {res};") v.add("\}") end + + redef fun new_visitor do return new SeparateCompilerVisitor(self) end # The C function associated to a methoddef separately compiled diff --git a/tests/base_types_formal_and_virtual3.nit b/tests/base_types_formal_and_virtual3.nit index 74d0366..d07d81d 100644 --- a/tests/base_types_formal_and_virtual3.nit +++ b/tests/base_types_formal_and_virtual3.nit @@ -32,3 +32,6 @@ class G[T: A] __debug__ type U: au end end + +interface Object +end diff --git a/tests/sav/nitg/fixme/base_attr_init_val2.res b/tests/sav/nitg/fixme/base_attr_init_val2.res deleted file mode 100644 index 7f85acf..0000000 --- a/tests/sav/nitg/fixme/base_attr_init_val2.res +++ /dev/null @@ -1 +0,0 @@ -Compilation error diff --git a/tests/sav/nitg/fixme/base_empty_module.res b/tests/sav/nitg/fixme/base_empty_module.res deleted file mode 100644 index 7f85acf..0000000 --- a/tests/sav/nitg/fixme/base_empty_module.res +++ /dev/null @@ -1 +0,0 @@ -Compilation error diff --git a/tests/sav/nitg/fixme/base_types_formal_and_virtual.res b/tests/sav/nitg/fixme/base_types_formal_and_virtual.res deleted file mode 100644 index 7f85acf..0000000 --- a/tests/sav/nitg/fixme/base_types_formal_and_virtual.res +++ /dev/null @@ -1 +0,0 @@ -Compilation error diff --git a/tests/sav/nitg/fixme/base_types_formal_and_virtual2.res b/tests/sav/nitg/fixme/base_types_formal_and_virtual2.res deleted file mode 100644 index 7f85acf..0000000 --- a/tests/sav/nitg/fixme/base_types_formal_and_virtual2.res +++ /dev/null @@ -1 +0,0 @@ -Compilation error diff --git a/tests/sav/nitg/fixme/base_types_formal_and_virtual3.res b/tests/sav/nitg/fixme/base_types_formal_and_virtual3.res deleted file mode 100644 index 7f85acf..0000000 --- a/tests/sav/nitg/fixme/base_types_formal_and_virtual3.res +++ /dev/null @@ -1 +0,0 @@ -Compilation error diff --git a/tests/sav/nitg/fixme/base_types_formal_and_virtual4.res b/tests/sav/nitg/fixme/base_types_formal_and_virtual4.res deleted file mode 100644 index 7f85acf..0000000 --- a/tests/sav/nitg/fixme/base_types_formal_and_virtual4.res +++ /dev/null @@ -1 +0,0 @@ -Compilation error diff --git a/tests/sav/nitg/fixme/error_needed_types.res b/tests/sav/nitg/fixme/error_needed_types.res deleted file mode 100644 index 7f85acf..0000000 --- a/tests/sav/nitg/fixme/error_needed_types.res +++ /dev/null @@ -1 +0,0 @@ -Compilation error -- 1.7.9.5