07c5277bdee2b45dfef1a7ccd626696b168809fc
[nit.git] / src / program.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 # Common things to represent a Nit program/library
18 package program
19
20 import metamodel
21 import icode
22 import primitive_info
23 import mmloader
24
25 redef class ToolContext
26 readable writable var _global: Bool = false
27 writable var _use_SFT_optimization: Bool = true
28
29 # We can say that we are using SFT optimization *only* when we are
30 # doing global compilation and we enabled the command line option
31 fun use_SFT_optimization: Bool do return global and _use_SFT_optimization
32 end
33
34 # Instances of this class represent a program/library that will
35 # be analyzed/compiled by nitc
36 class Program
37 # This is the ToolContext associated with this Program
38 # It contains (amongts other things) the command line options
39 readable var _tc: ToolContext
40
41 # This module is the 'main' module, the one where we find the 'main' method
42 readable var _module: MMModule
43
44 # This method is the entry point of this program
45 # There might be no entry point (if in fact we are compiling a library)
46 readable var _main_method: nullable MMMethod = null
47
48 # This is the class that contains the main method.
49 # Would be null if there is no main method
50 readable var _main_class: nullable MMLocalClass = null
51
52 # When we are using global compilation, we generate _glob files instead
53 # of _sep files so that we do not corrupt separate compilation
54 fun get_file_ending: String do return if tc.global then "_glob" else "_sep"
55
56 # This method will ensure that all the metamodel is computed before we
57 # start using all the classes
58 private fun finish_processing_classes do
59 var classes = new Array[MMLocalClass]
60 for c in module.local_classes do
61 c.compute_super_classes
62 classes.add(c)
63 end
64
65 for c in classes do
66 c.compute_ancestors
67 c.inherit_global_properties
68 end
69 end
70
71 fun compute_main_method do
72 # Check for the 'Sys' class
73 var sysname = once "Sys".to_symbol
74 if not module.has_global_class_named(sysname) then return
75 var sys = module.class_by_name(sysname)
76
77 # Check for 'Sys::main' method
78 var entryname = once "main".to_symbol
79 if not sys.has_global_property_by_name(entryname) then return
80
81 _main_method = sys.select_method(entryname)
82 _main_class = sys
83 end
84
85 # Generation of allocation function of this class
86 fun generate_allocation_iroutines
87 do
88 for c in module.local_classes do
89 var pi = c.primitive_info
90 if pi == null then
91 do
92 # Generate INIT_ATTRIBUTES routine
93 var iself = new IRegister(c.get_type)
94 var iselfa = [iself]
95 var iroutine = new IRoutine(iselfa, null)
96 var icb = new ICodeBuilder(module, iroutine)
97
98 for g in c.global_properties do
99 var p = c[g]
100 var t = p.signature.return_type
101 if p isa MMAttribute and t != null then
102 var ir = p.iroutine
103 if ir == null then continue
104 # FIXME: Not compatible with sep compilation
105 var e = icb.inline_routine(ir, iselfa, null).as(not null)
106 icb.stmt(new IAttrWrite(p, iself, e))
107 end
108 end
109
110 c.init_var_iroutine = iroutine
111 end
112 do
113 # Compile CHECKNAME
114 var iself = new IRegister(c.get_type)
115 var iselfa = [iself]
116 var iroutine = new IRoutine(iselfa, null)
117 var icb = new ICodeBuilder(module, iroutine)
118 for g in c.global_properties do
119 var p = c[g]
120 var t = p.signature.return_type
121 if p isa MMAttribute and t != null and not t.is_nullable then
122 icb.add_attr_check(p, iself)
123 end
124 end
125
126 c.checknew_iroutine = iroutine
127 end
128
129 for g in c.global_properties do
130 var p = c[g]
131 # FIXME skip invisible constructors
132 if not p.global.is_init_for(c) then continue
133 assert p isa MMMethod
134
135 var iself = new IRegister(c.get_type)
136 var iparams = new Array[IRegister]
137 for i in [0..p.signature.arity[ do iparams.add(new IRegister(p.signature[i]))
138 var iroutine = new IRoutine(iparams, iself)
139 iroutine.location = p.iroutine.location
140 var icb = new ICodeBuilder(module, iroutine)
141
142 var inew = new IAllocateInstance(c.get_type)
143 inew.result = iself
144 icb.stmt(inew)
145 var iargs = [iself]
146 iargs.add_all(iparams)
147
148 icb.stmt(new IInitAttributes(c.get_type, iself))
149 icb.stmt(new IStaticCall(p, iargs))
150 icb.stmt(new ICheckInstance(c.get_type, iself))
151
152 c.new_instance_iroutine[p] = iroutine
153 end
154 end
155 end
156 end
157
158 # This function will call the attached block for each IRoutines
159 # in this program
160 fun with_each_iroutines
161 !action(i: IRoutine, m: MMModule)
162 do
163 for m in module.mhe.greaters_and_self do
164 for c in m.local_classes do
165 var iroutine: nullable IRoutine = null
166
167 # Process methods and attributes initialization
168 for p in c.local_local_properties do
169 if p isa MMAttribute then
170 iroutine = p.iroutine
171 else if p isa MMMethod then
172 iroutine = p.iroutine
173 end
174 if iroutine == null then continue
175 action(iroutine, m)
176 end
177
178 # Process class-specific iroutines
179 iroutine = c.init_var_iroutine
180 if iroutine != null then
181 action(iroutine, m)
182 end
183 iroutine = c.checknew_iroutine
184 if iroutine != null then
185 action(iroutine, m)
186 end
187 for i in c.new_instance_iroutine do
188 action(i, m)
189 end
190 end
191 end
192 end
193
194 # This function will call the attached block for each MMMethods
195 # in this program
196 fun with_each_methods
197 !action(m: MMMethod)
198 do
199 for m in module.mhe.greaters_and_self do
200 for c in m.local_classes do
201 # Process methods and attributes initialization
202 for p in c.local_local_properties do
203 if p isa MMMethod then
204 action(p)
205 end
206 end
207 end
208 end
209 end
210
211 init(m: MMModule, toolcontext: ToolContext) do
212 _module = m
213 _tc = toolcontext
214 finish_processing_classes
215 end
216 end
217
218 redef class MMLocalClass
219 # IRoutine for the initialization of the default attributes (called by IInitAttributes)
220 readable writable var _init_var_iroutine: nullable IRoutine = null
221 # IRoutine to validate the instance after initialization (called by ICheckInstance)
222 readable writable var _checknew_iroutine: nullable IRoutine = null
223 # IRoutines to call to create a new valid instance (memory allocated, object initialized and validated)
224 # These iroutines will call: IAllocateInstance, IInitAttributes, some init function and ICheckInstance
225 # These routines will be called by INew
226 readable var _new_instance_iroutine: HashMap[MMMethod, IRoutine] = new HashMap[MMMethod, IRoutine]
227 end