update NOTICE and LICENSE
[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 private 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 _main_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 # This method will ensure that all the metamodel is computed before we
53 # start using all the classes
54 private fun finish_processing_classes do
55 var classes = new Array[MMLocalClass]
56 for c in main_module.local_classes do
57 c.compute_super_classes
58 classes.add(c)
59 end
60
61 for c in classes do
62 c.compute_ancestors
63 end
64 end
65
66 fun compute_main_method do
67 # Check for the 'Sys' class
68 var sysname = once "Sys".to_symbol
69 if not main_module.has_global_class_named(sysname) then return
70 var sys = main_module.class_by_name(sysname)
71
72 # Check for 'Sys::main' method
73 var entryname = once "main".to_symbol
74 if not sys.has_global_property_by_name(entryname) then return
75
76 _main_method = sys.select_method(entryname)
77 _main_class = sys
78 end
79
80 # Generation of allocation function of this class
81 fun generate_allocation_iroutines
82 do
83 for c in main_module.local_classes do
84 if c.global.is_abstract or c.global.is_interface then continue
85 var pi = c.primitive_info
86 if pi == null then
87 do
88 # Generate INIT_ATTRIBUTES routine
89 var iself = new IRegister(c.get_type)
90 var iselfa = [iself]
91 var iroutine = new IRoutine(iselfa, null)
92 var icb = new ICodeBuilder(main_module, iroutine)
93
94 for g in c.global_properties do
95 if not g.intro isa MMAttribute then continue
96 var p = c[g]
97 var t = p.signature.return_type
98 if p isa MMAttribute and t != null then
99 var ir = p.iroutine
100 if ir == null then continue
101 # FIXME: Not compatible with sep compilation
102 var e = icb.inline_routine(ir, iselfa, null).as(not null)
103 icb.stmt(new IAttrWrite(p, iself, e))
104 end
105 end
106
107 c.init_var_iroutine = iroutine
108 end
109 do
110 # Compile CHECKNAME
111 var iself = new IRegister(c.get_type)
112 var iselfa = [iself]
113 var iroutine = new IRoutine(iselfa, null)
114 var icb = new ICodeBuilder(main_module, iroutine)
115 for g in c.global_properties do
116 if not g.intro isa MMAttribute then continue
117 var p = c[g]
118 var t = p.signature.return_type
119 if p isa MMAttribute and t != null and not t.is_nullable then
120 icb.add_attr_check(p, iself)
121 end
122 end
123
124 c.checknew_iroutine = iroutine
125 end
126
127 for g in c.global_properties do
128 # FIXME skip invisible constructors
129 if not g.is_init_for(c) then continue
130 var p = c[g]
131 assert p isa MMMethod
132
133 var iself = new IRegister(c.get_type)
134 var iparams = new Array[IRegister]
135 for i in [0..p.signature.arity[ do iparams.add(new IRegister(p.signature[i]))
136 var iroutine = new IRoutine(iparams, iself)
137 iroutine.location = p.iroutine.location
138 var icb = new ICodeBuilder(main_module, iroutine)
139
140 var inew = new IAllocateInstance(c.get_type)
141 inew.result = iself
142 icb.stmt(inew)
143 var iargs = [iself]
144 iargs.add_all(iparams)
145
146 icb.stmt(new IInitAttributes(c.get_type, iself))
147 icb.stmt(new IStaticCall(p, iargs))
148 icb.stmt(new ICheckInstance(c.get_type, iself))
149
150 c.new_instance_iroutine[p] = iroutine
151 end
152 end
153 end
154 end
155
156 # This function will call the attached block for each IRoutines
157 # in this program
158 fun with_each_iroutines
159 !action(i: IRoutine, m: MMModule)
160 do
161 for m in main_module.mhe.greaters_and_self do
162 for c in m.local_classes do
163 var iroutine: nullable IRoutine = null
164
165 # Process methods and attributes initialization
166 for p in c.local_local_properties do
167 if p isa MMAttribute then
168 iroutine = p.iroutine
169 else if p isa MMMethod then
170 iroutine = p.iroutine
171 end
172 if iroutine == null then continue
173 action(iroutine, m)
174 end
175
176 # Process class-specific iroutines
177 iroutine = c.init_var_iroutine
178 if iroutine != null then
179 action(iroutine, m)
180 end
181 iroutine = c.checknew_iroutine
182 if iroutine != null then
183 action(iroutine, m)
184 end
185 for i in c.new_instance_iroutine do
186 action(i, m)
187 end
188 end
189 end
190 end
191
192 # This function will call the attached block for each MMMethods
193 # in this program
194 fun with_each_methods
195 !action(m: MMMethod)
196 do
197 for m in main_module.mhe.greaters_and_self do
198 for c in m.local_classes do
199 # Process methods and attributes initialization
200 for p in c.local_local_properties do
201 if p isa MMMethod then
202 action(p)
203 end
204 end
205 end
206 end
207 end
208
209 # This function will call the attached block for each live local classes
210 # in this program
211 fun with_each_live_local_classes
212 !action(m: MMLocalClass)
213 do
214 for c in main_module.local_classes do
215 action(c)
216 end
217 end
218
219 init(m: MMModule, toolcontext: ToolContext) do
220 _main_module = m
221 _tc = toolcontext
222 finish_processing_classes
223 end
224 end
225
226 redef class MMLocalClass
227 # IRoutine for the initialization of the default attributes (called by IInitAttributes)
228 readable writable var _init_var_iroutine: nullable IRoutine = null
229 # IRoutine to validate the instance after initialization (called by ICheckInstance)
230 readable writable var _checknew_iroutine: nullable IRoutine = null
231 # IRoutines to call to create a new valid instance (memory allocated, object initialized and validated)
232 # These iroutines will call: IAllocateInstance, IInitAttributes, some init function and ICheckInstance
233 # These routines will be called by INew
234 readable var _new_instance_iroutine: HashMap[MMMethod, IRoutine] = new HashMap[MMMethod, IRoutine]
235 end