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]
30 # Run all registered phases on a set of modules
31 fun run_phases
(nmodules
: Collection[AModule])
34 self.info
("*** SEMANTIC ANALYSIS ***", 1)
36 var phases
= self.phases
.to_a
37 self.phases
.sort
(phases
)
39 for phase
in phases
do
40 self.info
(" registered phases: {phase.class_name}", 2)
43 for nmodule
in nmodules
do
44 self.info
("Semantic analysis module {nmodule.location.file.filename}", 2)
45 for phase
in phases
do
46 self.info
(" phase: {phase.class_name}", 3)
47 assert phase
.toolcontext
== self
48 var errcount
= self.error_count
49 phase
.process_nmodule
(nmodule
)
50 if errcount
!= self.error_count
then
54 errcount
= self.error_count
55 for nclassdef
in nmodule
.n_classdefs
do
56 for npropdef
in nclassdef
.n_propdefs
do
57 self.info
(" phase: {phase.class_name} for {npropdef.location}", 4)
58 assert phase
.toolcontext
== self
59 phase
.process_npropdef
(npropdef
)
62 if errcount
!= self.error_count
then
71 self.info
("*** END SEMANTIC ANALYSIS: {time1-time0} ***", 2)
75 # Abstraction of steps in the analysis/processing of Nit programs
76 # Specific phases should implements either `process_nmodule` or
79 # The toolcontext instance attached to the phase
80 var toolcontext
: ToolContext
82 # The dependence relation of the phase with the other phases
83 var in_hierarchy
: POSetElement[Phase]
85 # Initialize and register a phase to the toolcontext
86 init(toolcontext
: ToolContext, depends
: nullable Collection[Phase])
88 self.toolcontext
= toolcontext
89 in_hierarchy
= toolcontext
.phases
.add_node
(self)
90 if depends
!= null then
92 toolcontext
.phases
.add_edge
(self, d
)
97 # Specific actions to execute on the whole tree of a module
99 fun process_nmodule
(nmodule
: AModule) do end
101 # Specific actions to execute on the tree of a property
102 # Note that the order of the visit is the one of the file
104 fun process_npropdef
(npropdef
: APropdef) do end