update NOTICE
[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 # --disable-phase
34 var opt_sloppy = new OptionBool("DEBUG: force lazy semantic analysis of the source-code", "--sloppy")
35
36 redef init
37 do
38 super
39
40 option_context.add_option(opt_disable_phase, opt_sloppy)
41 end
42
43 redef fun process_options(args)
44 do
45 super
46
47 for v in opt_disable_phase.value do
48 if v == "list" then
49 for p in phases_list do
50 var deps = p.in_hierarchy.direct_greaters
51 if deps.is_empty then
52 print p
53 else
54 print "{p} (dep: {deps.join(", ")})"
55 end
56 end
57 exit 0
58 end
59
60 var found = false
61 for p in phases do
62 if v != p.to_s then continue
63 found = true
64 p.disabled = true
65 end
66 if not found then fatal_error(null, "Error: no phase named `{v}`. Use `list` to list all phases.")
67 end
68
69 if opt_sloppy.value then semantize_is_lazy = true
70 end
71
72 # The list of registered phases in the application order.
73 var phases_list: Sequence[Phase] is lazy do
74 var phases = self.phases.to_a
75 self.phases.sort(phases)
76 return phases
77 end
78
79 # Is `phase_process_npropdef` not called automatically by `run_phases`?
80 #
81 # When set to true, it is the responsibility of the tools
82 #
83 # Is false by default.
84 var semantize_is_lazy = false is writable
85
86 # Set of already analyzed modules.
87 private var phased_modules = new HashSet[AModule]
88
89 # Run all registered phases on a set of modules
90 fun run_phases(nmodules: Collection[AModule])
91 do
92 var time0 = get_time
93 self.info("*** SEMANTIC ANALYSIS ***", 1)
94 #phases.show_dot
95
96 var phases = phases_list
97
98 for phase in phases do
99 self.info(" registered phases: {phase}", 2)
100 end
101
102 for nmodule in nmodules do
103 if phased_modules.has(nmodule) then continue
104 phased_modules.add nmodule
105
106 self.info("Semantic analysis module {nmodule.location.file.filename}", 2)
107
108 var vannot = new AnnotationPhaseVisitor
109 vannot.enter_visit(nmodule)
110
111 for phase in phases do
112 if phase.disabled then continue
113 self.info(" phase: {phase}", 3)
114 assert phase.toolcontext == self
115 var errcount = self.error_count
116 phase.process_nmodule(nmodule)
117 if errcount != self.error_count then
118 self.check_errors
119 break
120 end
121 errcount = self.error_count
122 for nclassdef in nmodule.n_classdefs do
123 assert phase.toolcontext == self
124 phase.process_nclassdef(nclassdef)
125 if not semantize_is_lazy then for npropdef in nclassdef.n_propdefs do
126 assert phase.toolcontext == self
127 phase_process_npropdef(phase, npropdef)
128 end
129 end
130 if errcount != self.error_count then
131 self.check_errors
132 break
133 end
134 for na in vannot.annotations do
135 phase.process_annotated_node(na.parent.parent.as(not null), na)
136 end
137 if errcount != self.error_count then
138 self.check_errors
139 break
140 end
141 end
142 self.check_errors
143 end
144
145 var time1 = get_time
146 self.info("*** END SEMANTIC ANALYSIS: {time1-time0} ***", 2)
147
148 errors_info
149 end
150
151 # Process the given `phase` on the `npropdef`
152 # Called by `run_phases`
153 protected fun phase_process_npropdef(phase: Phase, npropdef: APropdef)
154 do
155 phase.process_npropdef(npropdef)
156 end
157
158 # Run the phase on the given npropdef.
159 # Does nothing if `semantize_is_lazy` is false.
160 fun run_phases_on_npropdef(npropdef: APropdef)
161 do
162 if not semantize_is_lazy then return
163 if npropdef.is_phased then return
164 npropdef.is_phased = true
165
166 #self.info("Semantic analysis of property {npropdef.location.file.filename}", 0)
167
168 var phases = phases_list
169 for phase in phases do
170 if phase.disabled then continue
171 assert phase.toolcontext == self
172 phase_process_npropdef(phase, npropdef)
173 self.check_errors
174 end
175 end
176 end
177
178 redef class APropdef
179 # Is the propdef already analyzed by `run_phases_on_npropdef`.
180 # Unused unless `semantize_is_lazy` is true.
181 private var is_phased = false
182 end
183
184 # Collect all annotation
185 private class AnnotationPhaseVisitor
186 super Visitor
187
188 # The collected annotations
189 var annotations = new Array[AAnnotation]
190
191 redef fun visit(n)
192 do
193 n.visit_all(self)
194 if n isa AAnnotation then annotations.add n
195 end
196 end
197
198 # Abstraction of steps in the analysis/processing of Nit programs
199 # Specific phases should implements one of the `process_*` method
200 abstract class Phase
201 # The toolcontext instance attached to the phase
202 var toolcontext: ToolContext
203
204 # The dependence relation of the phase with the other phases
205 var in_hierarchy: POSetElement[Phase] is noinit
206
207 # The explicit dependences, used to initialize `in_importation`
208 var depends: nullable Collection[Phase]
209
210 # Initialize and register a phase to the toolcontext
211 init
212 do
213 in_hierarchy = toolcontext.phases.add_node(self)
214 var depends = self.depends
215 if depends != null then
216 for d in depends do
217 toolcontext.phases.add_edge(self, d)
218 end
219 end
220 end
221
222 # By default, the name is the lowercased prefix of the classname
223 redef fun to_s do return class_name.strip_extension("Phase").to_lower
224
225 # Is the phase globally disabled?
226 # A disabled phase is not called automatically called by `ToolContext::run_phases` and cie.
227 #
228 # Warning: disabling a phase may cause subsequent phases to work in a degraded way or to fail.
229 var disabled = false is writable
230
231 # Specific actions to execute on the whole tree of a module
232 # @toimplement
233 fun process_nmodule(nmodule: AModule) do end
234
235 # Specific actions to execute on the tree of a class definition
236 # Note that the order of the visit is the one of the file
237 # @toimplement
238 fun process_nclassdef(nclassdef: AClassdef) do end
239
240 # Specific actions to execute on the tree of a property
241 # Note that the order of the visit is the one of the file
242 # @toimplement
243 fun process_npropdef(npropdef: APropdef) do end
244
245 # Specific actions to execute on annotated nodes
246 # Note that the order of the visit is the one of the file
247 # @toimplement
248 fun process_annotated_node(node: ANode, nat: AAnnotation) do end
249 end