phase: make `disabled=` public
[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 dependance 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 fun phases_list: Sequence[Phase]
68 do
69 var phases = self.phases.to_a
70 self.phases.sort(phases)
71 return phases
72 end
73
74 # Set of already analyzed modules.
75 private var phased_modules = new HashSet[AModule]
76
77 # Run all registered phases on a set of modules
78 fun run_phases(nmodules: Collection[AModule])
79 do
80 var time0 = get_time
81 self.info("*** SEMANTIC ANALYSIS ***", 1)
82 #phases.show_dot
83
84 var phases = phases_list
85
86 for phase in phases do
87 self.info(" registered phases: {phase}", 2)
88 end
89
90 for nmodule in nmodules do
91 if phased_modules.has(nmodule) then continue
92 phased_modules.add nmodule
93
94 self.info("Semantic analysis module {nmodule.location.file.filename}", 2)
95
96 var vannot = new AnnotationPhaseVisitor
97 vannot.enter_visit(nmodule)
98
99 for phase in phases do
100 if phase.disabled then continue
101 self.info(" phase: {phase}", 3)
102 assert phase.toolcontext == self
103 var errcount = self.error_count
104 phase.process_nmodule(nmodule)
105 if errcount != self.error_count then
106 self.check_errors
107 break
108 end
109 errcount = self.error_count
110 for nclassdef in nmodule.n_classdefs do
111 assert phase.toolcontext == self
112 phase.process_nclassdef(nclassdef)
113 for npropdef in nclassdef.n_propdefs do
114 assert phase.toolcontext == self
115 phase_process_npropdef(phase, npropdef)
116 end
117 end
118 if errcount != self.error_count then
119 self.check_errors
120 break
121 end
122 for na in vannot.annotations do
123 phase.process_annotated_node(na.parent.parent.as(not null), na)
124 end
125 if errcount != self.error_count then
126 self.check_errors
127 break
128 end
129 end
130 self.check_errors
131 end
132
133 var time1 = get_time
134 self.info("*** END SEMANTIC ANALYSIS: {time1-time0} ***", 2)
135
136 errors_info
137 end
138
139 fun phase_process_npropdef(phase: Phase, npropdef: APropdef)
140 do
141 phase.process_npropdef(npropdef)
142 end
143 end
144
145 # Collect all annotation
146 private class AnnotationPhaseVisitor
147 super Visitor
148
149 # The collected annotations
150 var annotations = new Array[AAnnotation]
151
152 redef fun visit(n)
153 do
154 n.visit_all(self)
155 if n isa AAnnotation then annotations.add n
156 end
157 end
158
159 # Abstraction of steps in the analysis/processing of Nit programs
160 # Specific phases should implements one of the `process_*` method
161 abstract class Phase
162 # The toolcontext instance attached to the phase
163 var toolcontext: ToolContext
164
165 # The dependence relation of the phase with the other phases
166 var in_hierarchy: POSetElement[Phase]
167
168 # Initialize and register a phase to the toolcontext
169 init(toolcontext: ToolContext, depends: nullable Collection[Phase])
170 do
171 self.toolcontext = toolcontext
172 in_hierarchy = toolcontext.phases.add_node(self)
173 if depends != null then
174 for d in depends do
175 toolcontext.phases.add_edge(self, d)
176 end
177 end
178 end
179
180 # By default, the name is the lowercased prefix of the classname
181 redef fun to_s do return class_name.strip_extension("Phase").to_lower
182
183 # Is the phase globally disabled?
184 # A disabled phase is not called automatically called by `ToolContext::run_phases` and cie.
185 #
186 # Warning: disabling a phase may cause subsequent phases to work in a degraded way or to fail.
187 var disabled = false is writable
188
189 # Specific actions to execute on the whole tree of a module
190 # @toimplement
191 fun process_nmodule(nmodule: AModule) do end
192
193 # Specific actions to execute on the tree of a class definition
194 # Note that the order of the visit is the one of the file
195 # @toimplement
196 fun process_nclassdef(nclassdef: AClassdef) do end
197
198 # Specific actions to execute on the tree of a property
199 # Note that the order of the visit is the one of the file
200 # @toimplement
201 fun process_npropdef(npropdef: APropdef) do end
202
203 # Specific actions to execute on annotated nodes
204 # Note that the order of the visit is the one of the file
205 # @toimplement
206 fun process_annotated_node(node: ANode, nat: AAnnotation) do end
207 end