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("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 # The list of registered phases in the application order.
68 fun phases_list
: Sequence[Phase]
70 var phases
= self.phases
.to_a
71 self.phases
.sort
(phases
)
75 # Set of already analyzed modules.
76 private var phased_modules
= new HashSet[AModule]
78 # Run all registered phases on a set of modules
79 fun run_phases
(nmodules
: Collection[AModule])
82 self.info
("*** SEMANTIC ANALYSIS ***", 1)
85 var phases
= phases_list
87 for phase
in phases
do
88 self.info
(" registered phases: {phase}", 2)
91 for nmodule
in nmodules
do
92 if phased_modules
.has
(nmodule
) then continue
93 phased_modules
.add nmodule
95 self.info
("Semantic analysis module {nmodule.location.file.filename}", 2)
97 var vannot
= new AnnotationPhaseVisitor
98 vannot
.enter_visit
(nmodule
)
100 for phase
in phases
do
101 if phase
.disabled
then continue
102 self.info
(" phase: {phase}", 3)
103 assert phase
.toolcontext
== self
104 var errcount
= self.error_count
105 phase
.process_nmodule
(nmodule
)
106 if errcount
!= self.error_count
then
110 errcount
= self.error_count
111 for nclassdef
in nmodule
.n_classdefs
do
112 assert phase
.toolcontext
== self
113 phase
.process_nclassdef
(nclassdef
)
114 for npropdef
in nclassdef
.n_propdefs
do
115 assert phase
.toolcontext
== self
116 phase_process_npropdef
(phase
, npropdef
)
119 if errcount
!= self.error_count
then
123 for na
in vannot
.annotations
do
124 phase
.process_annotated_node
(na
.parent
.parent
.as(not null), na
)
126 if errcount
!= self.error_count
then
135 self.info
("*** END SEMANTIC ANALYSIS: {time1-time0} ***", 2)
140 # Process the given `phase` on the `npropdef`
141 # Called by `run_phases`
142 protected fun phase_process_npropdef
(phase
: Phase, npropdef
: APropdef)
144 phase
.process_npropdef
(npropdef
)
148 # Collect all annotation
149 private class AnnotationPhaseVisitor
152 # The collected annotations
153 var annotations
= new Array[AAnnotation]
158 if n
isa AAnnotation then annotations
.add n
162 # Abstraction of steps in the analysis/processing of Nit programs
163 # Specific phases should implements one of the `process_*` method
165 # The toolcontext instance attached to the phase
166 var toolcontext
: ToolContext
168 # The dependence relation of the phase with the other phases
169 var in_hierarchy
: POSetElement[Phase] is noinit
171 # The explicit dependences, used to initialize `in_importation`
172 var depends
: nullable Collection[Phase]
174 # Initialize and register a phase to the toolcontext
177 in_hierarchy
= toolcontext
.phases
.add_node
(self)
178 var depends
= self.depends
179 if depends
!= null then
181 toolcontext
.phases
.add_edge
(self, d
)
186 # By default, the name is the lowercased prefix of the classname
187 redef fun to_s
do return class_name
.strip_extension
("Phase").to_lower
189 # Is the phase globally disabled?
190 # A disabled phase is not called automatically called by `ToolContext::run_phases` and cie.
192 # Warning: disabling a phase may cause subsequent phases to work in a degraded way or to fail.
193 var disabled
= false is writable
195 # Specific actions to execute on the whole tree of a module
197 fun process_nmodule
(nmodule
: AModule) do end
199 # Specific actions to execute on the tree of a class definition
200 # Note that the order of the visit is the one of the file
202 fun process_nclassdef
(nclassdef
: AClassdef) do end
204 # Specific actions to execute on the tree of a property
205 # Note that the order of the visit is the one of the file
207 fun process_npropdef
(npropdef
: APropdef) do end
209 # Specific actions to execute on annotated nodes
210 # Note that the order of the visit is the one of the file
212 fun process_annotated_node
(node
: ANode, nat
: AAnnotation) do end