1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Common things to represent a Nit program/library
25 redef class ToolContext
26 readable writable var _global
: Bool = false
27 writable var _use_SFT_optimization
: Bool = true
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
34 # Instances of this class represent a program/library that will
35 # be analyzed/compiled by nitc
37 # This is the ToolContext associated with this Program
38 # It contains (amongts other things) the command line options
39 readable var _tc
: ToolContext
41 # This module is the 'main' module, the one where we find the 'main' method
42 readable var _module
: MMModule
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
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
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 module.local_classes
do
57 c
.compute_super_classes
66 fun compute_main_method
do
67 # Check for the 'Sys' class
68 var sysname
= once
"Sys".to_symbol
69 if not module.has_global_class_named
(sysname
) then return
70 var sys
= module.class_by_name
(sysname
)
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
76 _main_method
= sys
.select_method
(entryname
)
80 # Generation of allocation function of this class
81 fun generate_allocation_iroutines
83 for c
in module.local_classes
do
84 var pi
= c
.primitive_info
87 # Generate INIT_ATTRIBUTES routine
88 var iself
= new IRegister(c
.get_type
)
90 var iroutine
= new IRoutine(iselfa
, null)
91 var icb
= new ICodeBuilder(module, iroutine
)
93 for g
in c
.global_properties
do
94 if not g
.intro
isa MMAttribute then continue
96 var t
= p
.signature
.return_type
97 if p
isa MMAttribute and t
!= null then
99 if ir
== null then continue
100 # FIXME: Not compatible with sep compilation
101 var e
= icb
.inline_routine
(ir
, iselfa
, null).as(not null)
102 icb
.stmt
(new IAttrWrite(p
, iself
, e
))
106 c
.init_var_iroutine
= iroutine
110 var iself
= new IRegister(c
.get_type
)
112 var iroutine
= new IRoutine(iselfa
, null)
113 var icb
= new ICodeBuilder(module, iroutine
)
114 for g
in c
.global_properties
do
115 if not g
.intro
isa MMAttribute then continue
117 var t
= p
.signature
.return_type
118 if p
isa MMAttribute and t
!= null and not t
.is_nullable
then
119 icb
.add_attr_check
(p
, iself
)
123 c
.checknew_iroutine
= iroutine
126 for g
in c
.global_properties
do
127 # FIXME skip invisible constructors
128 if not g
.is_init_for
(c
) then continue
130 assert p
isa MMMethod
132 var iself
= new IRegister(c
.get_type
)
133 var iparams
= new Array[IRegister]
134 for i
in [0..p
.signature
.arity
[ do iparams
.add
(new IRegister(p
.signature
[i
]))
135 var iroutine
= new IRoutine(iparams
, iself
)
136 iroutine
.location
= p
.iroutine
.location
137 var icb
= new ICodeBuilder(module, iroutine
)
139 var inew
= new IAllocateInstance(c
.get_type
)
143 iargs
.add_all
(iparams
)
145 icb
.stmt
(new IInitAttributes(c
.get_type
, iself
))
146 icb
.stmt
(new IStaticCall(p
, iargs
))
147 icb
.stmt
(new ICheckInstance(c
.get_type
, iself
))
149 c
.new_instance_iroutine
[p
] = iroutine
155 # This function will call the attached block for each IRoutines
157 fun with_each_iroutines
158 !action
(i
: IRoutine, m
: MMModule)
160 for m
in module.mhe
.greaters_and_self
do
161 for c
in m
.local_classes
do
162 var iroutine
: nullable IRoutine = null
164 # Process methods and attributes initialization
165 for p
in c
.local_local_properties
do
166 if p
isa MMAttribute then
167 iroutine
= p
.iroutine
168 else if p
isa MMMethod then
169 iroutine
= p
.iroutine
171 if iroutine
== null then continue
175 # Process class-specific iroutines
176 iroutine
= c
.init_var_iroutine
177 if iroutine
!= null then
180 iroutine
= c
.checknew_iroutine
181 if iroutine
!= null then
184 for i
in c
.new_instance_iroutine
do
191 # This function will call the attached block for each MMMethods
193 fun with_each_methods
196 for m
in module.mhe
.greaters_and_self
do
197 for c
in m
.local_classes
do
198 # Process methods and attributes initialization
199 for p
in c
.local_local_properties
do
200 if p
isa MMMethod then
208 # This function will call the attached block for each live local classes
210 fun with_each_live_local_classes
211 !action
(m
: MMLocalClass)
213 for c
in module.local_classes
do
218 init(m
: MMModule, toolcontext
: ToolContext) do
221 finish_processing_classes
225 redef class MMLocalClass
226 # IRoutine for the initialization of the default attributes (called by IInitAttributes)
227 readable writable var _init_var_iroutine
: nullable IRoutine = null
228 # IRoutine to validate the instance after initialization (called by ICheckInstance)
229 readable writable var _checknew_iroutine
: nullable IRoutine = null
230 # IRoutines to call to create a new valid instance (memory allocated, object initialized and validated)
231 # These iroutines will call: IAllocateInstance, IInitAttributes, some init function and ICheckInstance
232 # These routines will be called by INew
233 readable var _new_instance_iroutine
: HashMap[MMMethod, IRoutine] = new HashMap[MMMethod, IRoutine]