tests: nitg can compile empty modules
authorJean Privat <jean@pryen.org>
Thu, 29 Nov 2012 14:08:10 +0000 (09:08 -0500)
committerJean Privat <jean@pryen.org>
Thu, 29 Nov 2012 19:01:39 +0000 (14:01 -0500)
create a new method compile_main_function to factorize the work

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

src/global_compiler.nit
src/separate_compiler.nit
tests/base_types_formal_and_virtual3.nit
tests/sav/nitg/fixme/base_attr_init_val2.res [deleted file]
tests/sav/nitg/fixme/base_empty_module.res [deleted file]
tests/sav/nitg/fixme/base_types_formal_and_virtual.res [deleted file]
tests/sav/nitg/fixme/base_types_formal_and_virtual2.res [deleted file]
tests/sav/nitg/fixme/base_types_formal_and_virtual3.res [deleted file]
tests/sav/nitg/fixme/base_types_formal_and_virtual4.res [deleted file]
tests/sav/nitg/fixme/error_needed_types.res [deleted file]

index a67bd61..610d5fc 100644 (file)
@@ -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
 
index b63c1f3..b533884 100644 (file)
@@ -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
index 74d0366..d07d81d 100644 (file)
@@ -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 (file)
index 7f85acf..0000000
+++ /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 (file)
index 7f85acf..0000000
+++ /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 (file)
index 7f85acf..0000000
+++ /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 (file)
index 7f85acf..0000000
+++ /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 (file)
index 7f85acf..0000000
+++ /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 (file)
index 7f85acf..0000000
+++ /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 (file)
index 7f85acf..0000000
+++ /dev/null
@@ -1 +0,0 @@
-Compilation error