src: finish documenting some module
[nit.git] / src / phase.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Phases of the processing of nit programs
16 module phase
17
18 import toolcontext
19 import parser
20 import poset
21
22 redef class ToolContext
23 # The various registered phases to performs
24 # The order in the poset is the dependence of phases
25 #
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]
29
30 # --disable-phase
31 var opt_disable_phase = new OptionArray("DEBUG: Disable a specific phase; use `list` to get the list.", "--disable-phase")
32
33 redef init
34 do
35 super
36
37 option_context.add_option(opt_disable_phase)
38 end
39
40 redef fun process_options(args)
41 do
42 super
43
44 for v in opt_disable_phase.value do
45 if v == "list" then
46 for p in phases_list do
47 var deps = p.in_hierarchy.direct_greaters
48 if deps.is_empty then
49 print p
50 else
51 print "{p} (dep: {deps.join(", ")})"
52 end
53 end
54 exit 0
55 end
56
57 var found = false
58 for p in phases do
59 if v != p.to_s then continue
60 found = true
61 p.disabled = true
62 end
63 if not found then fatal_error(null, "Error: no phase named `{v}`. Use `list` to list all phases.")
64 end
65 end
66
67 # The list of registered phases in the application order.
68 fun phases_list: Sequence[Phase]
69 do
70 var phases = self.phases.to_a
71 self.phases.sort(phases)
72 return phases
73 end
74
75 # Set of already analyzed modules.
76 private var phased_modules = new HashSet[AModule]
77
78 # Run all registered phases on a set of modules
79 fun run_phases(nmodules: Collection[AModule])
80 do
81 var time0 = get_time
82 self.info("*** SEMANTIC ANALYSIS ***", 1)
83 #phases.show_dot
84
85 var phases = phases_list
86
87 for phase in phases do
88 self.info(" registered phases: {phase}", 2)
89 end
90
91 for nmodule in nmodules do
92 if phased_modules.has(nmodule) then continue
93 phased_modules.add nmodule
94
95 self.info("Semantic analysis module {nmodule.location.file.filename}", 2)
96
97 var vannot = new AnnotationPhaseVisitor
98 vannot.enter_visit(nmodule)
99
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
107 self.check_errors
108 break
109 end
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)
117 end
118 end
119 if errcount != self.error_count then
120 self.check_errors
121 break
122 end
123 for na in vannot.annotations do
124 phase.process_annotated_node(na.parent.parent.as(not null), na)
125 end
126 if errcount != self.error_count then
127 self.check_errors
128 break
129 end
130 end
131 self.check_errors
132 end
133
134 var time1 = get_time
135 self.info("*** END SEMANTIC ANALYSIS: {time1-time0} ***", 2)
136
137 errors_info
138 end
139
140 # Process the given `phase` on the `npropdef`
141 # Called by `run_phases`
142 protected fun phase_process_npropdef(phase: Phase, npropdef: APropdef)
143 do
144 phase.process_npropdef(npropdef)
145 end
146 end
147
148 # Collect all annotation
149 private class AnnotationPhaseVisitor
150 super Visitor
151
152 # The collected annotations
153 var annotations = new Array[AAnnotation]
154
155 redef fun visit(n)
156 do
157 n.visit_all(self)
158 if n isa AAnnotation then annotations.add n
159 end
160 end
161
162 # Abstraction of steps in the analysis/processing of Nit programs
163 # Specific phases should implements one of the `process_*` method
164 abstract class Phase
165 # The toolcontext instance attached to the phase
166 var toolcontext: ToolContext
167
168 # The dependence relation of the phase with the other phases
169 var in_hierarchy: POSetElement[Phase] is noinit
170
171 # The explicit dependences, used to initialize `in_importation`
172 var depends: nullable Collection[Phase]
173
174 # Initialize and register a phase to the toolcontext
175 init
176 do
177 in_hierarchy = toolcontext.phases.add_node(self)
178 var depends = self.depends
179 if depends != null then
180 for d in depends do
181 toolcontext.phases.add_edge(self, d)
182 end
183 end
184 end
185
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
188
189 # Is the phase globally disabled?
190 # A disabled phase is not called automatically called by `ToolContext::run_phases` and cie.
191 #
192 # Warning: disabling a phase may cause subsequent phases to work in a degraded way or to fail.
193 var disabled = false is writable
194
195 # Specific actions to execute on the whole tree of a module
196 # @toimplement
197 fun process_nmodule(nmodule: AModule) do end
198
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
201 # @toimplement
202 fun process_nclassdef(nclassdef: AClassdef) do end
203
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
206 # @toimplement
207 fun process_npropdef(npropdef: APropdef) do end
208
209 # Specific actions to execute on annotated nodes
210 # Note that the order of the visit is the one of the file
211 # @toimplement
212 fun process_annotated_node(node: ANode, nat: AAnnotation) do end
213 end