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 dependance 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("DEBUG: Disable a specific phase; use `list` to get the list.", "--disable-phase")
37 option_context
.add_option
(opt_disable_phase
)
40 redef fun process_options
(args
)
44 for v
in opt_disable_phase
.value
do
46 for p
in phases_list
do
47 var deps
= p
.in_hierarchy
.direct_greaters
51 print
"{p} (dep: {deps.join(", ")})"
59 if v
!= p
.to_s
then continue
63 if not found
then fatal_error
(null, "Error: no phase named `{v}`. Use `list` to list all phases.")
67 fun phases_list
: Sequence[Phase]
69 var phases
= self.phases
.to_a
70 self.phases
.sort
(phases
)
74 # Is `phase_process_npropdef` not called automatically by `run_phases`?
76 # When set to true, it is the responsibility of the tools
78 # Is false by default.
79 var semantize_is_lazy
= false is writable
81 # Set of already analyzed modules.
82 private var phased_modules
= new HashSet[AModule]
84 # Run all registered phases on a set of modules
85 fun run_phases
(nmodules
: Collection[AModule])
88 self.info
("*** SEMANTIC ANALYSIS ***", 1)
91 var phases
= phases_list
93 for phase
in phases
do
94 self.info
(" registered phases: {phase}", 2)
97 for nmodule
in nmodules
do
98 if phased_modules
.has
(nmodule
) then continue
99 phased_modules
.add nmodule
101 self.info
("Semantic analysis module {nmodule.location.file.filename}", 2)
103 var vannot
= new AnnotationPhaseVisitor
104 vannot
.enter_visit
(nmodule
)
106 for phase
in phases
do
107 if phase
.disabled
then continue
108 self.info
(" phase: {phase}", 3)
109 assert phase
.toolcontext
== self
110 var errcount
= self.error_count
111 phase
.process_nmodule
(nmodule
)
112 if errcount
!= self.error_count
then
116 errcount
= self.error_count
117 for nclassdef
in nmodule
.n_classdefs
do
118 assert phase
.toolcontext
== self
119 phase
.process_nclassdef
(nclassdef
)
120 if not semantize_is_lazy
then for npropdef
in nclassdef
.n_propdefs
do
121 assert phase
.toolcontext
== self
122 phase_process_npropdef
(phase
, npropdef
)
125 if errcount
!= self.error_count
then
129 for na
in vannot
.annotations
do
130 phase
.process_annotated_node
(na
.parent
.parent
.as(not null), na
)
132 if errcount
!= self.error_count
then
141 self.info
("*** END SEMANTIC ANALYSIS: {time1-time0} ***", 2)
146 fun phase_process_npropdef
(phase
: Phase, npropdef
: APropdef)
148 phase
.process_npropdef
(npropdef
)
151 # Run the phase on the given npropdef.
152 # Does nothing if `semantize_is_lazy` is false.
153 fun run_phases_on_npropdef
(npropdef
: APropdef)
155 if not semantize_is_lazy
then return
156 if npropdef
.is_phased
then return
157 npropdef
.is_phased
= true
159 #self.info("Semantic analysis of property {npropdef.location.file.filename}", 0)
161 var phases
= phases_list
162 for phase
in phases
do
163 if phase
.disabled
then continue
164 assert phase
.toolcontext
== self
165 phase_process_npropdef
(phase
, npropdef
)
172 # Is the propdef already analyzed by `run_phases_on_npropdef`.
173 # Unused unless `semantize_is_lazy` is true.
174 private var is_phased
= false
177 # Collect all annotation
178 private class AnnotationPhaseVisitor
181 # The collected annotations
182 var annotations
= new Array[AAnnotation]
187 if n
isa AAnnotation then annotations
.add n
191 # Abstraction of steps in the analysis/processing of Nit programs
192 # Specific phases should implements one of the `process_*` method
194 # The toolcontext instance attached to the phase
195 var toolcontext
: ToolContext
197 # The dependence relation of the phase with the other phases
198 var in_hierarchy
: POSetElement[Phase] is noinit
200 # The explicit dependences, used to initialize `in_importation`
201 var depends
: nullable Collection[Phase]
203 # Initialize and register a phase to the toolcontext
206 in_hierarchy
= toolcontext
.phases
.add_node
(self)
207 var depends
= self.depends
208 if depends
!= null then
210 toolcontext
.phases
.add_edge
(self, d
)
215 # By default, the name is the lowercased prefix of the classname
216 redef fun to_s
do return class_name
.strip_extension
("Phase").to_lower
218 # Is the phase globally disabled?
219 # A disabled phase is not called automatically called by `ToolContext::run_phases` and cie.
221 # Warning: disabling a phase may cause subsequent phases to work in a degraded way or to fail.
222 var disabled
= false is writable
224 # Specific actions to execute on the whole tree of a module
226 fun process_nmodule
(nmodule
: AModule) do end
228 # Specific actions to execute on the tree of a class definition
229 # Note that the order of the visit is the one of the file
231 fun process_nclassdef
(nclassdef
: AClassdef) do end
233 # Specific actions to execute on the tree of a property
234 # Note that the order of the visit is the one of the file
236 fun process_npropdef
(npropdef
: APropdef) do end
238 # Specific actions to execute on annotated nodes
239 # Note that the order of the visit is the one of the file
241 fun process_annotated_node
(node
: ANode, nat
: AAnnotation) do end