0ef4b4af78a177fb4b75ad76681cb931abe288db
[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 scope
22 import flow
23
24 redef class APropdef
25 # Entry point of the whole local variable initialization verifier
26 fun do_local_var_init(toolcontext: ToolContext)
27 do
28 var v = new LocalVarInitVisitor(toolcontext)
29 v.enter_visit(self)
30 end
31 end
32
33 private class LocalVarInitVisitor
34 super Visitor
35
36 var toolcontext: ToolContext
37
38 init(toolcontext: ToolContext)
39 do
40 self.toolcontext = toolcontext
41 end
42
43 # Local variables that are possibily unset (ie local variable without an initial value)
44 var maybe_unset_vars: Set[Variable] = new HashSet[Variable]
45
46 fun mark_is_unset(node: AExpr, variable: nullable Variable)
47 do
48 assert variable != null
49 self.maybe_unset_vars.add(variable)
50 end
51
52 fun mark_is_set(node: AExpr, variable: nullable Variable)
53 do
54 assert variable != null
55 if not maybe_unset_vars.has(variable) then return
56
57 var flow = node.after_flow_context.as(not null)
58 flow.set_vars.add(variable)
59 end
60
61 fun check_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 if not flow.is_variable_set(variable) then
68 self.toolcontext.error(node.hot_location, "Error: variable '{variable}' is possibly unset.")
69 # Remove the variable to avoid repetting errors
70 self.maybe_unset_vars.remove(variable)
71 end
72 end
73
74 redef fun visit(n)
75 do
76 if n != null then n.accept_local_var_visitor(self)
77 end
78 end
79
80 redef class FlowContext
81 private var set_vars: Set[Variable] = new HashSet[Variable]
82
83 private fun is_variable_set(variable: Variable): Bool
84 do
85 if self.set_vars.has(variable) then return true
86 var previous = self.previous
87 if previous.length == 0 then return false
88 if previous.length == 1 then return previous.first.is_variable_set(variable)
89 for p in self.previous do
90 if not p.is_variable_set(variable) then
91 return false
92 end
93 end
94 # Cache the result
95 self.set_vars.add(variable)
96 return true
97 end
98 end
99
100 redef class ANode
101 private fun accept_local_var_visitor(v: LocalVarInitVisitor) do self.visit_all(v)
102 end
103
104 redef class AVardeclExpr
105 redef fun accept_local_var_visitor(v)
106 do
107 super
108 # The variable is unset only if there is no initial value.
109
110 # Note: loops in inital value are not a problem
111 # Example:
112 #
113 # var foo = foo + 1 #-> Error during typing: "self.foo" unknown
114 #
115 # var foo
116 # foo = foo + 1 #-> Error here because 'foo' is possibly unset
117 if self.n_expr == null then
118 v.mark_is_unset(self, self.variable)
119 end
120 end
121 end
122
123 redef class AVarExpr
124 redef fun accept_local_var_visitor(v)
125 do
126 super
127 v.check_is_set(self, self.variable)
128 end
129 end
130
131 redef class AVarAssignExpr
132 redef fun accept_local_var_visitor(v)
133 do
134 super
135 v.mark_is_set(self, self.variable)
136 end
137 end
138
139 redef class AVarReassignExpr
140 redef fun accept_local_var_visitor(v)
141 do
142 super
143 v.check_is_set(self, self.variable)
144 end
145 end