Merge: introduce nit_env.sh to setup the shell environement
[nit.git] / src / model / model_visitor.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 # Simple visitor framework for Nit models.
16 #
17 # This module provides the `ModelVisitor` abstract class and
18 # refines the classes of the `MEntity` hierarchy to add visiting related methods.
19 #
20 # A standard approach on complex visitor is to dispatch the `ModelVisitor::visit` on the
21 # entities to do specific things.
22 #
23 # ~~~nitish
24 # class FooVisitor
25 # super ModelVisitor
26 # redef fun visit(e) do e.foo_visit(self)
27 # end
28 #
29 # redef class MEntity
30 # do
31 # fun foo_vist(v: FooVisitor) do visit_all(v)
32 # end
33 #
34 # redef class MClass
35 # do
36 # redef fun foo_visit(v) do
37 # print self
38 # super
39 # end
40 # end
41 # ~~~
42 module model_visitor
43
44 import model
45
46 # The abstract model visitor template.
47 #
48 # Specific visitor must implement the `visit` method to perform the work.
49 abstract class ModelVisitor
50 # Visit the entity `e`.
51 #
52 # This method setups `current_entity` and call `visit`.
53 # If `e` is null, nothing is done.
54 fun enter_visit(e: nullable MEntity) do
55 if e == null then return
56 if e.is_fictive and not include_fictive then return
57 var old_entity = current_entity
58 current_entity = e
59 visit(e)
60 current_entity = old_entity
61 end
62
63 # The current visited entity
64 var current_entity: nullable MEntity = null
65
66 # Method to define in specific visitor.
67 #
68 # It should not be called directly but used by `enter_visit`
69 protected fun visit(e: MEntity) is abstract
70
71 # Filter classes and method on the visibility.
72 #
73 # If set, only the classes and method with at least the given
74 # visibility level will be visited.
75 var min_visibility: nullable MVisibility = null is writable
76
77 # Is `visibility` acceptable with regard to `min_visibility`?
78 private fun accept_visitibily(visibility: MVisibility): Bool
79 do
80 var min = min_visibility
81 return min == null or min <= visibility
82 end
83
84 # Include fictive entities?
85 #
86 # By default, fictive entities (see `MEntity::is_fictive`) are not visited.
87 var include_fictive = false is writable
88 end
89
90 redef class MEntity
91 # Call `v.enter_visit` on all nested entities.
92 #
93 # See the specific implementation in the subclasses.
94 fun visit_all(v: ModelVisitor) do end
95 end
96
97 redef class Model
98 # Visit all the packages of the model.
99 redef fun visit_all(v) do
100 for x in mpackages do v.enter_visit(x)
101 end
102 end
103
104 redef class MPackage
105 # Visit the root group of the package.
106 redef fun visit_all(v) do
107 v.enter_visit(root)
108 end
109 end
110
111 redef class MGroup
112 # Visit all the subgroups and modules of the group.
113 redef fun visit_all(v) do
114 for x in in_nesting.direct_smallers do v.enter_visit(x)
115 for x in mmodules do v.enter_visit(x)
116 end
117 end
118
119 redef class MModule
120 # Visit all the classes and class definitions of the module.
121 #
122 # On class introduction, the `MClass` then the `MClassDef` are visited.
123 # On class refinement, only the `MClassDef` is visited (the `MClass` is visited in an imported module).
124 # On class importation, nothing is visited (the `MClass` and the `MClassDef` are visited in imported modules).
125 redef fun visit_all(v) do
126 for x in mclassdefs do
127 if not v.accept_visitibily(x.mclass.visibility) then return
128 if x.is_intro then v.enter_visit(x.mclass)
129 v.enter_visit(x)
130 end
131 end
132 end
133
134 redef class MClassDef
135 # Visit all the classes and class definitions of the module.
136 #
137 # On property introduction, the `MProperty` then the `MPropDef` are visited.
138 # On property redefinition, only the `MPropDef` is visited (the `MProperty` is visited in an inherited class).
139 # On property inheritance, nothing is visited (the `MProperty` and the `MPropDef` are visited in inherited classes).
140 redef fun visit_all(v) do
141 for x in mpropdefs do
142 if not v.accept_visitibily(x.mproperty.visibility) then return
143 if x.is_intro then v.enter_visit(x.mproperty)
144 v.enter_visit(x)
145 end
146 end
147 end