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 fun phases_list
: Sequence[Phase]
32 var phases
= self.phases
.to_a
33 self.phases
.sort
(phases
)
37 # Run all registered phases on a set of modules
38 fun run_phases
(nmodules
: Collection[AModule])
41 self.info
("*** SEMANTIC ANALYSIS ***", 1)
44 var phases
= phases_list
46 for phase
in phases
do
47 self.info
(" registered phases: {phase.class_name}", 2)
50 for nmodule
in nmodules
do
51 self.info
("Semantic analysis module {nmodule.location.file.filename}", 2)
52 for phase
in phases
do
53 self.info
(" phase: {phase.class_name}", 3)
54 assert phase
.toolcontext
== self
55 var errcount
= self.error_count
56 phase
.process_nmodule
(nmodule
)
57 if errcount
!= self.error_count
then
61 errcount
= self.error_count
62 for nclassdef
in nmodule
.n_classdefs
do
63 for npropdef
in nclassdef
.n_propdefs
do
64 self.info
(" phase: {phase.class_name} for {npropdef.location}", 4)
65 assert phase
.toolcontext
== self
66 phase
.process_npropdef
(npropdef
)
69 if errcount
!= self.error_count
then
73 var v
= new AnnotationPhaseVisitor(phase
)
74 v
.enter_visit
(nmodule
)
75 if errcount
!= self.error_count
then
84 self.info
("*** END SEMANTIC ANALYSIS: {time1-time0} ***", 2)
88 private class AnnotationPhaseVisitor
93 init(phase
: Phase) do self.phase
= phase
98 if n
isa AAnnotation then phase
.process_annotated_node
(n
.parent
.parent
.as(not null), n
)
102 # Abstraction of steps in the analysis/processing of Nit programs
103 # Specific phases should implements one of the `process_*` method
105 # The toolcontext instance attached to the phase
106 var toolcontext
: ToolContext
108 # The dependence relation of the phase with the other phases
109 var in_hierarchy
: POSetElement[Phase]
111 # Initialize and register a phase to the toolcontext
112 init(toolcontext
: ToolContext, depends
: nullable Collection[Phase])
114 self.toolcontext
= toolcontext
115 in_hierarchy
= toolcontext
.phases
.add_node
(self)
116 if depends
!= null then
118 toolcontext
.phases
.add_edge
(self, d
)
123 # Specific actions to execute on the whole tree of a module
125 fun process_nmodule
(nmodule
: AModule) do end
127 # Specific actions to execute on the tree of a property
128 # Note that the order of the visit is the one of the file
130 fun process_npropdef
(npropdef
: APropdef) do end
132 # Specific actions to execute on annotated nodes
133 # Note that the order of the visit is the one of the file
135 fun process_annotated_node
(node
: ANode, nat
: AAnnotation) do end