nit: restrict some module visibility
[nit.git] / src / compiling / icode_generator.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
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 # Generates ICode as an output format
18 package icode_generator
19
20 private import compiling_base
21 import program
22 private import analysis
23 private import primitive_info
24
25 redef class Program
26 # Generates ICode for the whole program
27 fun generate_icode_files do
28 tc.compdir.mkdir
29
30 with_each_live_local_classes !action(c) do
31 c.generate_icode_file(tc.compdir.as(not null))
32 end
33 end
34 end
35
36 # A class to dump ICode to a file
37 class FileICodeDumper
38 super ICodeDumper
39 var _file: OFStream
40
41 init(f: OFStream) do
42 # We don't want to output line numbers and locations
43 super(false, false)
44 _file = f
45 end
46
47 redef fun write(s) do
48 for i in [0..indent_level[ do
49 _file.write(" ")
50 end
51 _file.write(s)
52 _file.write("\n")
53 end
54 end
55
56 redef class MMLocalClass
57 # Generates ICode for all properties of this class in a file
58 fun generate_icode_file(dir: String) do
59 var file = new OFStream.open("{dir}/{self}.icode")
60 if primitive_info == null then
61 # Initialization methods
62 if init_var_iroutine != null then
63 var icd = new FileICodeDumper(file)
64 icd.indent
65 file.write("Init var iroutine::\n")
66 init_var_iroutine.dump(icd)
67 file.write("\n\n")
68 end
69 if checknew_iroutine != null then
70 var icd = new FileICodeDumper(file)
71 icd.indent
72 file.write("Check new instance iroutine::\n")
73 checknew_iroutine.dump(icd)
74 file.write("\n\n")
75 end
76
77 # 'new' methods
78 for pg in global_properties do
79 if not pg.is_init_for(self) then continue
80 var p = self[pg]
81 assert p isa MMMethod
82 var icd = new FileICodeDumper(file)
83 icd.indent
84 file.write("New instance:: {p.full_name}\n")
85 new_instance_iroutine[p].dump(icd)
86 file.write("\n\n")
87 end
88 end
89
90 # Other methods
91 for pg in global_properties do
92 var p = self[pg]
93 if p.local_class == self and p isa MMMethod then
94 p.generate_icode(file)
95 end
96 end
97 file.close
98 end
99 end
100
101 redef class MMMethod
102 # Generates ICode for this method with a little header
103 fun generate_icode(file: OFStream) do
104 var icd = new FileICodeDumper(file)
105 icd.indent
106 file.write("Method:: {full_name}\n")
107 iroutine.dump(icd)
108 file.write("\n\n")
109 end
110 end