4a8cfb46258fdf0e8e7e1f2736291e9752153169
[nit.git] / src / frontend / div_by_zero.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 # Detection of divisions by zero in obvious cases
16 # This module is only an example of how to implements a simple phase in the nit tools
17 module div_by_zero
18
19 import literal # Because we need the real value of literal in the AST
20 import semantize # Because we need the static type of receivers
21
22 # Note, `ToolContext` is the main overlord class
23 # Among its jobs, it orchestrates and executes the various phases
24 redef class ToolContext
25 # Instantiate a new specific phase `div_by_zero_phase`
26 # It must be executed after the two phases `literal_phase` and `typing_phase`
27 # Those two phases are statically known since they are introduced by the modules `literal` and `typing`
28 # Note that the constructor automatically register the phase to the ToolContext; this explains why there is `self`
29 var div_by_zero_phase: Phase = new DivByZeroPhase(self, [literal_phase, typing_phase])
30 end
31
32 # OK, let we create a specific phase
33 private class DivByZeroPhase
34 super Phase
35
36 # Specific phases just have to implement the `process_nmodule` method.
37 redef fun process_nmodule(nmodule)
38 do
39 # The AST node is not enough, we need also the associated model element
40 var mmodule = nmodule.mmodule.as(not null)
41 # For the specific job we have, the simpler it to launch a visitor on
42 # all elements of the AST.
43 var visitor = new DivByZeroVisitor(toolcontext, mmodule)
44 visitor.enter_visit(nmodule)
45 end
46 end
47
48 # The real visitor that does the real job
49 private class DivByZeroVisitor
50 super Visitor
51
52 # The toolcontext is our entree point to most services
53 var toolcontext: ToolContext
54
55 # The mmodule is the current module
56 var mmodule: MModule
57
58 redef fun visit(node)
59 do
60 # Recursively visit all sub-nodes
61 node.visit_all(self)
62
63 # Now just filter out until we get what we want...
64
65 # 1. We need a `/` operation
66 if not node isa ASlashExpr then return
67
68 # 2. The second operand must be a integer literal
69 var op2 = node.n_expr2
70 if not op2 isa AIntExpr then return
71
72 # 3. Its value must be 0
73 # Note: because of `literal_phase` the `value` method exists
74 if op2.value != 0 then return
75
76 # 4. The receiver (first operand) must be an Int
77 var op1 = node.n_expr
78 var int_type = mmodule.get_primitive_class("Int").mclass_type
79 if not op1.mtype.is_subtype(mmodule, null, int_type) then return
80
81 # Error detected
82 toolcontext.error(node.location, "Error: Definitely division by zero")
83 end
84 end