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