Scope & Typing: visits the catch part of a do ... catch ... end
[nit.git] / src / uml / uml_class.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 # Provides facilities of exporting a `Model` to a UML class diagram
16 module uml_class
17
18 import uml_base
19 import model::model_collect
20
21 redef class UMLModel
22 # Generates a UML class diagram from a `Model`
23 fun generate_class_uml: Writable do
24 var tpl = new Template
25 tpl.add "digraph G \{\n"
26 tpl.add """ fontname = "Bitstream Vera Sans"
27 fontsize = 8
28 node [
29 fontname = "Bitstream Vera Sans"
30 fontsize = 8
31 shape = "record"
32 ]
33
34 edge [
35 fontname = "Bitstream Vera Sans"
36 fontsize = 8
37 ]\n"""
38 for mclass in view.mclasses do
39 tpl.add mclass.tpl_class(self)
40 tpl.add "\n"
41 end
42 tpl.add "\}"
43 return tpl
44 end
45 end
46
47 redef class MEntity
48 # Generates a dot-compatible `Writable` UML Class diagram from `self`
49 fun tpl_class(model: UMLModel): Writable is abstract
50 end
51
52 redef class MClass
53
54 redef fun tpl_class(model) do
55 var t = new Template
56 t.add "{name} [\n label = \"\{"
57 if kind == abstract_kind then
58 t.add "abstract\\n{name}"
59 else if kind == interface_kind then
60 t.add "interface\\n{name}"
61 else
62 t.add "{name}"
63 end
64 if arity > 0 then
65 t.add "["
66 t.add mparameters.first.name
67 for i in [1 .. mparameters.length[ do
68 t.add ", "
69 t.add mparameters[i].name
70 end
71 t.add "]"
72 end
73 t.add "|"
74 var props = collect_intro_mproperties(model.view)
75 for i in props do
76 if not i isa MAttribute then continue
77 t.add i.tpl_class(model)
78 t.add "\\l"
79 end
80 t.add "|"
81 for i in props do
82 if not i isa MMethod then continue
83 t.add i.tpl_class(model)
84 t.add "\\l"
85 end
86 t.add "\}\"\n]\n"
87 var g = in_hierarchy(model.mainmodule).direct_greaters
88 for i in g do
89 if not model.view.accept_mentity(i) then continue
90 t.add "{i.name} -> {name} [dir=back"
91 if i.kind == interface_kind then
92 t.add " arrowtail=open style=dashed"
93 else
94 t.add " arrowtail=empty"
95 end
96 t.add "];\n"
97 end
98 return t
99 end
100
101 end
102
103 redef class MMethod
104 redef fun tpl_class(model) do
105 var tpl = new Template
106 tpl.add visibility.tpl_class
107 tpl.add " "
108 tpl.add name.escape_to_dot
109 tpl.add intro.msignature.tpl_class(model)
110 return tpl
111 end
112 end
113
114 redef class MSignature
115
116 redef fun tpl_class(model) do
117 var t = new Template
118 t.add "("
119 var params = new Array[MParameter]
120 for i in mparameters do
121 params.add i
122 end
123 if params.length > 0 then
124 t.add params.first.name
125 t.add ": "
126 t.add params.first.mtype.tpl_class(model)
127 for i in [1 .. params.length [ do
128 t.add ", "
129 t.add params[i].name
130 t.add ": "
131 t.add params[i].mtype.tpl_class(model)
132 end
133 end
134 t.add ")"
135 if return_mtype != null then
136 t.add ": "
137 t.add return_mtype.tpl_class(model)
138 end
139 return t
140 end
141 end
142
143 redef class MAttribute
144 redef fun tpl_class(model) do
145 var tpl = new Template
146 tpl.add visibility.tpl_class
147 tpl.add " "
148 tpl.add name
149 tpl.add ": "
150 tpl.add intro.static_mtype.tpl_class(model)
151 return tpl
152 end
153 end
154
155 redef class MVisibility
156 # Returns the visibility as a UML token
157 #
158 # assert public_visibility.tpl_class == "+"
159 # assert private_visibility.tpl_class == "-"
160 fun tpl_class: Writable do
161 if self == private_visibility then
162 return "-"
163 else if self == protected_visibility then
164 return "#"
165 else if self == public_visibility then
166 return "+"
167 else
168 return "+"
169 end
170 end
171 end
172
173 redef class MClassType
174 redef fun tpl_class(model) do
175 return name
176 end
177 end
178
179 redef class MGenericType
180 redef fun tpl_class(model) do
181 var t = new Template
182 t.add name.substring(0, name.index_of('['))
183 t.add "["
184 t.add arguments.first.tpl_class(model)
185 for i in [1 .. arguments.length[ do
186 t.add ", "
187 t.add arguments[i].tpl_class(model)
188 end
189 t.add "]"
190 return t
191 end
192 end
193
194 redef class MParameterType
195 redef fun tpl_class(model) do
196 return name
197 end
198 end
199
200 redef class MVirtualType
201 redef fun tpl_class(model) do
202 return name
203 end
204 end
205
206 redef class MNullableType
207 redef fun tpl_class(model) do
208 var t = new Template
209 t.add "nullable "
210 t.add mtype.tpl_class(model)
211 return t
212 end
213 end