nit: restrict some module visibility
[nit.git] / src / compiling / compiling.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Compute and generate tables for classes and modules.
18 package compiling
19
20 import table_computation
21 import compiling_base
22 private import icode_generator
23 private import compiling_global
24 private import compiling_icode
25 private import analysis
26
27 redef class Program
28 # The type of code generation to use
29 readable writable var _output_format: String = "none"
30
31 # Compile the program depending on the output format
32 fun compile_prog
33 do
34 if output_format == "none" then
35 # Nothing to do
36 else
37 # Optimize all iroutines
38 with_each_iroutines !action(i, m) do i.optimize(m)
39
40 if output_format == "C" then
41 compile_prog_to_c
42 else if output_format == "icode" then
43 generate_icode_files
44 end
45 end
46 end
47
48 # Compile the program to C
49 # Generate all files (_sep.[ch] or _glob.[ch]), the main file (_table.c) and the build file (_build.sh)
50 # Then execute the build.sh
51 fun compile_prog_to_c
52 do
53 var cprogram = new CProgram(self)
54
55 cprogram.compdir.mkdir
56
57 cprogram.files.add("$CLIBDIR/nit_main.c")
58 cprogram.files.add("$CLIBDIR/gc.c")
59 cprogram.files.add("$CLIBDIR/gc_static_objects_list.c")
60
61 tc.info("Generating C code",1)
62 for m in main_module.mhe.greaters_and_self do m.compile_separate_module(cprogram)
63
64 tc.info("Generating main, tables and makefile ...",1)
65 compile_main(cprogram)
66
67 cprogram.generate_build_file
68
69 if not tc.no_cc then cprogram.run_c_compiler
70 end
71
72 # Compile the main file
73 private fun compile_main(cprogram: CProgram)
74 do
75 var v = new CompilerVisitor(main_module, cprogram)
76 v.add_decl("#include <nit_common.h>")
77 compile_tables_to_c(v)
78 compile_main_part(v)
79 var filename = "{cprogram.compdir}/{main_module.cname}._tables.c"
80 cprogram.files.add(filename)
81 var f = new OFStream.open(filename)
82 f.write("/* This C file is generated by NIT to compile program {main_module.cname}. */\n")
83 for m in main_module.mhe.greaters_and_self do
84 f.write("#include \"{cprogram.module_header_name(m)}\"\n")
85 end
86 v.header_writer.write_to_stream(f)
87 v.writer.write_to_stream(f)
88 f.close
89 end
90 end
91
92 redef class MMModule
93 # Compile the sep or glob files (of the current module only)
94 private fun compile_separate_module(cprogram: CProgram)
95 do
96 var tc = cprogram.program.tc
97 tc.info("Generating C code for module: {full_name}",2)
98 var v = new CompilerVisitor(self, cprogram)
99 v.add_decl("#include <nit_common.h>")
100
101 var native_name = location.file.filename.strip_extension(".nit")
102 var native_header = native_name + "_nit.h"
103 if native_header.file_exists then
104 v.add_decl("#include <{native_header.basename("")}>")
105 cprogram.include_dirs.add("-I {native_name.dirname}")
106 end
107 var native_body = native_name + "_nit.c"
108 if native_body.file_exists then cprogram.files.add(native_body)
109
110 declare_class_tables_to_c(v)
111 compile_mod_to_c(v)
112
113 var hfilename = cprogram.module_header_name(self)
114 var f = new OFStream.open("{cprogram.compdir}/{hfilename}")
115 f.write("/* This C header file is generated by NIT to compile modules and programs that requires {full_name}. */\n")
116 f.write("#ifndef {cname}{cprogram.get_file_ending}\n")
117 f.write("#define {cname}{cprogram.get_file_ending}\n")
118 for m in mhe.direct_greaters do f.write("#include \"{cprogram.module_header_name(m)}\"\n")
119 v.header_writer.write_to_stream(f)
120 f.write("#endif\n")
121 f.close
122
123 var cfilename = "{cprogram.compdir}/{cname}.{cprogram.get_file_ending}.c"
124 cprogram.files.add(cfilename)
125 f = new OFStream.open("{cfilename}")
126 f.write("/* This C file is generated by NIT to compile module {cname}. */\n")
127 f.write("#include \"{hfilename}\"\n")
128 v.top_writer.write_to_stream(f)
129 f.close
130 end
131 end
132