1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Phases of the processing of nit programs
22 redef class ToolContext
23 # The various registered phases to performs
24 # The order in the poset is the dependence of phases
26 # While you can directly modify the poset (nodes and edges),
27 # it is often simpler to use the constructor in `Phase`
28 var phases
= new POSet[Phase]
31 var opt_disable_phase
= new OptionArray("Disable a specific phase; use `list` to get the list (debug)", "--disable-phase")
34 var opt_sloppy
= new OptionBool("Force lazy semantic analysis of the source-code (debug)", "--sloppy")
40 option_context
.add_option
(opt_disable_phase
, opt_sloppy
)
43 redef fun process_options
(args
)
47 for v
in opt_disable_phase
.value
do
49 for p
in phases_list
do
50 var deps
= p
.in_hierarchy
.direct_greaters
54 print
"{p} (dep: {deps.join(", ")})"
62 if v
!= p
.to_s
then continue
66 if not found
then fatal_error
(null, "Error: no phase named `{v}`. Use `list` to list all phases.")
69 if opt_sloppy
.value
then semantize_is_lazy
= true
72 # The list of registered phases in the application order.
73 var phases_list
: Sequence[Phase] is lazy
do
74 var phases
= self.phases
.to_a
75 self.phases
.sort
(phases
)
79 # Is `phase_process_npropdef` not called automatically by `run_phases`?
81 # When set to true, it is the responsibility of the tools
83 # Is false by default.
84 var semantize_is_lazy
= false is writable
86 # Set of already analyzed modules.
87 private var phased_modules
= new HashSet[AModule]
89 # Run all registered phases on a set of modules
90 fun run_phases
(nmodules
: Collection[AModule])
93 self.info
("*** SEMANTIC ANALYSIS ***", 1)
96 var phases
= phases_list
98 for phase
in phases
do
99 self.info
(" registered phases: {phase}", 2)
102 for nmodule
in nmodules
do
103 if phased_modules
.has
(nmodule
) then continue
104 phased_modules
.add nmodule
106 self.info
("Semantic analysis module {nmodule.location.file.filename}", 2)
108 var vannot
= new AnnotationPhaseVisitor
109 vannot
.enter_visit
(nmodule
)
111 for phase
in phases
do
112 if phase
.disabled
then continue
113 assert phase
.toolcontext
== self
114 var errcount
= self.error_count
115 phase
.process_nmodule
(nmodule
)
116 if errcount
!= self.error_count
then
119 errcount
= self.error_count
120 for nclassdef
in nmodule
.n_classdefs
do
121 assert phase
.toolcontext
== self
122 phase
.process_nclassdef
(nclassdef
)
123 if not semantize_is_lazy
then for npropdef
in nclassdef
.n_propdefs
do
124 assert phase
.toolcontext
== self
125 phase_process_npropdef
(phase
, npropdef
)
128 if errcount
!= self.error_count
then
131 for na
in vannot
.annotations
do
133 if p
isa AAnnotations then p
= p
.parent
135 phase
.process_annotated_node
(p
, na
)
137 if errcount
!= self.error_count
then
145 self.info
("*** END SEMANTIC ANALYSIS: {time1-time0} ***", 2)
150 # Process the given `phase` on the `npropdef`
151 # Called by `run_phases`
152 protected fun phase_process_npropdef
(phase
: Phase, npropdef
: APropdef)
154 phase
.process_npropdef
(npropdef
)
157 # Run the phase on the given npropdef.
158 # Does nothing if `semantize_is_lazy` is false.
159 fun run_phases_on_npropdef
(npropdef
: APropdef)
161 if not semantize_is_lazy
then return
162 if npropdef
.is_phased
then return
163 npropdef
.is_phased
= true
165 #self.info("Semantic analysis of property {npropdef.location.file.filename}", 0)
167 var phases
= phases_list
168 for phase
in phases
do
169 if phase
.disabled
then continue
170 assert phase
.toolcontext
== self
171 phase_process_npropdef
(phase
, npropdef
)
178 # Is the propdef already analyzed by `run_phases_on_npropdef`.
179 # Unused unless `semantize_is_lazy` is true.
180 private var is_phased
= false
183 # Collect all annotation
184 private class AnnotationPhaseVisitor
187 # The collected annotations
188 var annotations
= new Array[AAnnotation]
193 if n
isa AAnnotation then annotations
.add n
197 # Abstraction of steps in the analysis/processing of Nit programs
198 # Specific phases should implements one of the `process_*` method
200 # The toolcontext instance attached to the phase
201 var toolcontext
: ToolContext
203 # The dependence relation of the phase with the other phases
204 var in_hierarchy
: POSetElement[Phase] is noinit
206 # The explicit dependences, used to initialize `in_importation`
207 var depends
: nullable Collection[Phase]
209 # Initialize and register a phase to the toolcontext
212 in_hierarchy
= toolcontext
.phases
.add_node
(self)
213 var depends
= self.depends
214 if depends
!= null then
216 toolcontext
.phases
.add_edge
(self, d
)
221 # By default, the name is the lowercased prefix of the classname
222 redef fun to_s
do return class_name
.strip_extension
("Phase").to_snake_case
224 # Is the phase globally disabled?
225 # A disabled phase is not called automatically called by `ToolContext::run_phases` and cie.
227 # Warning: disabling a phase may cause subsequent phases to work in a degraded way or to fail.
228 var disabled
= false is writable
230 # Specific actions to execute on the whole tree of a module
232 fun process_nmodule
(nmodule
: AModule) do end
234 # Specific actions to execute on the tree of a class definition
235 # Note that the order of the visit is the one of the file
237 fun process_nclassdef
(nclassdef
: AClassdef) do end
239 # Specific actions to execute on the tree of a property
240 # Note that the order of the visit is the one of the file
242 fun process_npropdef
(npropdef
: APropdef) do end
244 # Specific actions to execute on annotated nodes
245 # Note that the order of the visit is the one of the file
247 fun process_annotated_node
(node
: ANode, nat
: AAnnotation) do end