src: update most tools to new constructors
[nit.git] / src / semantize / local_var_init.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Verify that local variables are initialized before their usage
18 # Require that the scope and the flow analysis are already performed
19 module local_var_init
20
21 import flow
22
23 redef class ToolContext
24 var local_var_init_phase: Phase = new LocalVarInitPhase(self, [flow_phase])
25 end
26
27 private class LocalVarInitPhase
28 super Phase
29
30 redef fun process_npropdef(npropdef) do npropdef.do_local_var_init(toolcontext)
31 end
32
33 redef class APropdef
34 # Entry point of the whole local variable initialization verifier
35 fun do_local_var_init(toolcontext: ToolContext)
36 do
37 var v = new LocalVarInitVisitor(toolcontext)
38 v.enter_visit(self)
39 end
40 end
41
42 private class LocalVarInitVisitor
43 super Visitor
44
45 var toolcontext: ToolContext
46
47 # Local variables that are possibly unset (ie local variable without an initial value)
48 var maybe_unset_vars: Set[Variable] = new HashSet[Variable]
49
50 fun mark_is_unset(node: AExpr, variable: nullable Variable)
51 do
52 assert variable != null
53 self.maybe_unset_vars.add(variable)
54 end
55
56 fun mark_is_set(node: AExpr, variable: nullable Variable)
57 do
58 assert variable != null
59 if not maybe_unset_vars.has(variable) then return
60
61 var flow = node.after_flow_context.as(not null)
62 flow.set_vars.add(variable)
63 end
64
65 fun check_is_set(node: AExpr, variable: nullable Variable)
66 do
67 assert variable != null
68 if not maybe_unset_vars.has(variable) then return
69
70 var flow = node.after_flow_context.as(not null)
71 if not flow.is_variable_set(variable) then
72 self.toolcontext.error(node.hot_location, "Error: variable '{variable}' is possibly unset.")
73 # Remove the variable to avoid repeating errors
74 self.maybe_unset_vars.remove(variable)
75 end
76 end
77
78 redef fun visit(n)
79 do
80 n.accept_local_var_visitor(self)
81 end
82 end
83
84 redef class FlowContext
85 private var set_vars: Set[Variable] = new HashSet[Variable]
86
87 private fun is_variable_set(variable: Variable): Bool
88 do
89 if self.set_vars.has(variable) then return true
90 var previous = self.previous
91 if previous.length == 0 then return false
92 if previous.length == 1 then return previous.first.is_variable_set(variable)
93 for p in self.previous do
94 if not p.is_variable_set(variable) then
95 return false
96 end
97 end
98 # Cache the result
99 self.set_vars.add(variable)
100 return true
101 end
102 end
103
104 redef class ANode
105 private fun accept_local_var_visitor(v: LocalVarInitVisitor) do self.visit_all(v)
106 end
107
108 redef class AVardeclExpr
109 redef fun accept_local_var_visitor(v)
110 do
111 super
112 # The variable is unset only if there is no initial value.
113
114 # Note: loops in initial value are not a problem
115 # Example:
116 #
117 # var foo = foo + 1 #-> Error during typing: "self.foo" unknown
118 #
119 # var foo
120 # foo = foo + 1 #-> Error here because 'foo' is possibly unset
121 if self.n_expr == null then
122 v.mark_is_unset(self, self.variable)
123 end
124 end
125 end
126
127 redef class AVarExpr
128 redef fun accept_local_var_visitor(v)
129 do
130 super
131 v.check_is_set(self, self.variable)
132 end
133 end
134
135 redef class AVarAssignExpr
136 redef fun accept_local_var_visitor(v)
137 do
138 super
139 v.mark_is_set(self, self.variable)
140 end
141 end
142
143 redef class AVarReassignExpr
144 redef fun accept_local_var_visitor(v)
145 do
146 super
147 v.check_is_set(self, self.variable)
148 end
149 end