4b9a4ab40856b6ed7aaba3109e92ce2dc5483b5d
[nit.git] / src / 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 analaysis 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 init(toolcontext: ToolContext)
48 do
49 self.toolcontext = toolcontext
50 end
51
52 # Local variables that are possibily unset (ie local variable without an initial value)
53 var maybe_unset_vars: Set[Variable] = new HashSet[Variable]
54
55 fun mark_is_unset(node: AExpr, variable: nullable Variable)
56 do
57 assert variable != null
58 self.maybe_unset_vars.add(variable)
59 end
60
61 fun mark_is_set(node: AExpr, variable: nullable Variable)
62 do
63 assert variable != null
64 if not maybe_unset_vars.has(variable) then return
65
66 var flow = node.after_flow_context.as(not null)
67 flow.set_vars.add(variable)
68 end
69
70 fun check_is_set(node: AExpr, variable: nullable Variable)
71 do
72 assert variable != null
73 if not maybe_unset_vars.has(variable) then return
74
75 var flow = node.after_flow_context.as(not null)
76 if not flow.is_variable_set(variable) then
77 self.toolcontext.error(node.hot_location, "Error: variable '{variable}' is possibly unset.")
78 # Remove the variable to avoid repetting errors
79 self.maybe_unset_vars.remove(variable)
80 end
81 end
82
83 redef fun visit(n)
84 do
85 n.accept_local_var_visitor(self)
86 end
87 end
88
89 redef class FlowContext
90 private var set_vars: Set[Variable] = new HashSet[Variable]
91
92 private fun is_variable_set(variable: Variable): Bool
93 do
94 if self.set_vars.has(variable) then return true
95 var previous = self.previous
96 if previous.length == 0 then return false
97 if previous.length == 1 then return previous.first.is_variable_set(variable)
98 for p in self.previous do
99 if not p.is_variable_set(variable) then
100 return false
101 end
102 end
103 # Cache the result
104 self.set_vars.add(variable)
105 return true
106 end
107 end
108
109 redef class ANode
110 private fun accept_local_var_visitor(v: LocalVarInitVisitor) do self.visit_all(v)
111 end
112
113 redef class AVardeclExpr
114 redef fun accept_local_var_visitor(v)
115 do
116 super
117 # The variable is unset only if there is no initial value.
118
119 # Note: loops in inital value are not a problem
120 # Example:
121 #
122 # var foo = foo + 1 #-> Error during typing: "self.foo" unknown
123 #
124 # var foo
125 # foo = foo + 1 #-> Error here because 'foo' is possibly unset
126 if self.n_expr == null then
127 v.mark_is_unset(self, self.variable)
128 end
129 end
130 end
131
132 redef class AVarExpr
133 redef fun accept_local_var_visitor(v)
134 do
135 super
136 v.check_is_set(self, self.variable)
137 end
138 end
139
140 redef class AVarAssignExpr
141 redef fun accept_local_var_visitor(v)
142 do
143 super
144 v.mark_is_set(self, self.variable)
145 end
146 end
147
148 redef class AVarReassignExpr
149 redef fun accept_local_var_visitor(v)
150 do
151 super
152 v.check_is_set(self, self.variable)
153 end
154 end