modelize: advice on missing documentation
[nit.git] / src / modelize / modelize_class.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 # Analysis and verification of class definitions to instantiate model element
18 module modelize_class
19
20 import modelbuilder
21
22 redef class ToolContext
23 var modelize_class_phase: Phase = new ModelizeClassPhase(self, null)
24 end
25
26 private class ModelizeClassPhase
27 super Phase
28
29 redef fun process_nmodule(nmodule)
30 do
31 toolcontext.modelbuilder.build_classes(nmodule)
32 end
33 end
34
35 redef class ModelBuilder
36 # Visit the AST and create the `MClass` objects
37 private fun build_a_mclass(nmodule: AModule, nclassdef: AClassdef)
38 do
39 var mmodule = nmodule.mmodule.as(not null)
40
41 var name: String
42 var nkind: nullable AClasskind
43 var mkind: MClassKind
44 var nvisibility: nullable AVisibility
45 var mvisibility: nullable MVisibility
46 var arity = 0
47 if nclassdef isa AStdClassdef then
48 name = nclassdef.n_id.text
49 nkind = nclassdef.n_classkind
50 mkind = nkind.mkind
51 nvisibility = nclassdef.n_visibility
52 mvisibility = nvisibility.mvisibility
53 arity = nclassdef.n_formaldefs.length
54 if mvisibility == protected_visibility then
55 error(nvisibility, "Error: only properties can be protected.")
56 return
57 else if mvisibility == intrude_visibility then
58 error(nvisibility, "Error: intrude is not a legal visibility for classes.")
59 return
60 end
61 else if nclassdef isa ATopClassdef then
62 name = "Object"
63 nkind = null
64 mkind = interface_kind
65 nvisibility = null
66 mvisibility = public_visibility
67 else if nclassdef isa AMainClassdef then
68 name = "Sys"
69 nkind = null
70 mkind = concrete_kind
71 nvisibility = null
72 mvisibility = public_visibility
73 else
74 abort
75 end
76
77 var mclass = try_get_mclass_by_name(nclassdef, mmodule, name)
78 if mclass == null then
79 if nclassdef isa AStdClassdef and nclassdef.n_kwredef != null then
80 error(nclassdef, "Redef error: No imported class {name} to refine.")
81 return
82 end
83 mclass = new MClass(mmodule, name, arity, mkind, mvisibility)
84 #print "new class {mclass}"
85 else if nclassdef isa AStdClassdef and nmodule.mclass2nclassdef.has_key(mclass) then
86 error(nclassdef, "Error: A class {name} is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
87 return
88 else if nclassdef isa AStdClassdef and nclassdef.n_kwredef == null then
89 error(nclassdef, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
90 return
91 else if mclass.arity != arity then
92 error(nclassdef, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
93 return
94 else if nkind != null and mkind != concrete_kind and mclass.kind != mkind then
95 error(nkind, "Error: refinement changed the kind from a {mclass.kind} to a {mkind}")
96 else if nvisibility != null and mvisibility != public_visibility and mclass.visibility != mvisibility then
97 error(nvisibility, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
98 end
99 nclassdef.mclass = mclass
100 if not nmodule.mclass2nclassdef.has_key(mclass) then
101 nmodule.mclass2nclassdef[mclass] = nclassdef
102 nclassdef.all_defs = [nclassdef]
103 else
104 nmodule.mclass2nclassdef[mclass].all_defs.add(nclassdef)
105 end
106 end
107
108 # Visit the AST and create the `MClassDef` objects
109 private fun build_a_mclassdef(nmodule: AModule, nclassdef: AClassdef)
110 do
111 var mmodule = nmodule.mmodule.as(not null)
112 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
113 var mclass = nclassdef.mclass
114 if mclass == null then return # Skip error
115
116 # In case of non-standard AClassdef, try to attach to an already existing mclassdef
117 var other_nclassdef = nmodule.mclass2nclassdef[mclass]
118 if other_nclassdef != nclassdef then
119 assert not nclassdef isa AStdClassdef
120 nclassdef.mclassdef = other_nclassdef.mclassdef
121 return
122 end
123
124 var names = new Array[String]
125 var bounds = new Array[MType]
126 if nclassdef isa AStdClassdef and mclass.arity > 0 then
127 # Collect formal parameter names
128 for i in [0..mclass.arity[ do
129 var nfd = nclassdef.n_formaldefs[i]
130 var ptname = nfd.n_id.text
131 if names.has(ptname) then
132 error(nfd, "Error: A formal parameter type `{ptname}' already exists")
133 return
134 end
135 for c in ptname.chars do if c >= 'a' and c<= 'z' then
136 warning(nfd, "formal-type-name", "Warning: lowercase in the formal parameter type {ptname}")
137 break
138 end
139 names.add(ptname)
140 nfd.mtype = mclass.mclass_type.arguments[i].as(MParameterType)
141 end
142
143 # Revolve bound for formal parameter names
144 for i in [0..mclass.arity[ do
145 var nfd = nclassdef.n_formaldefs[i]
146 var nfdt = nfd.n_type
147 if nfdt != null then
148 var bound = resolve_mtype_unchecked(mmodule, null, nfdt, false)
149 if bound == null then return # Forward error
150 if bound.need_anchor then
151 # No F-bounds!
152 error(nfd, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
153 else
154 bounds.add(bound)
155 nfd.bound = bound
156 end
157 if bound isa MClassType and bound.mclass.kind == enum_kind then
158 warning(nfdt, "useless-bound", "Warning: Useless formal parameter type since `{bound}` cannnot have subclasses.")
159 end
160 else if mclass.mclassdefs.is_empty then
161 # No bound, then implicitely bound by nullable Object
162 var bound = objectclass.mclass_type.as_nullable
163 bounds.add(bound)
164 nfd.bound = bound
165 else
166 # Inherit the bound
167 var bound = mclass.intro.bound_mtype.arguments[i]
168 bounds.add(bound)
169 nfd.bound = bound
170 end
171 end
172 end
173
174 var bound_mtype = mclass.get_mtype(bounds)
175 var mclassdef = new MClassDef(mmodule, bound_mtype, nclassdef.location, names)
176 nclassdef.mclassdef = mclassdef
177 self.mclassdef2nclassdef[mclassdef] = nclassdef
178
179 if nclassdef isa AStdClassdef then
180 var ndoc = nclassdef.n_doc
181 if ndoc != null then
182 var mdoc = ndoc.to_mdoc
183 mclassdef.mdoc = mdoc
184 mdoc.original_mentity = mclassdef
185 else if mclassdef.is_intro and mclass.visibility >= public_visibility then
186 advice(nclassdef, "missing-doc", "Documentation warning: Undocumented public class `{mclass}`")
187 end
188 end
189
190 if mclassdef.is_intro then
191 self.toolcontext.info("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
192 else
193 self.toolcontext.info("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
194 end
195 end
196
197 # Visit the AST and set the super-types of the `MClassDef` objects
198 private fun collect_a_mclassdef_inheritance(nmodule: AModule, nclassdef: AClassdef)
199 do
200 var mmodule = nmodule.mmodule.as(not null)
201 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
202 var pointerclass = try_get_mclass_by_name(nmodule, mmodule, "Pointer")
203 var mclass = nclassdef.mclass.as(not null)
204 var mclassdef = nclassdef.mclassdef.as(not null)
205
206 # Do we need to specify Object as a super class?
207 var specobject = true
208
209 # Do we need to specify Pointer as a super class? (is only valid
210 # if `nclassdef` is an extern class)
211 var specpointer = true
212
213 var supertypes = new Array[MClassType]
214 if nclassdef isa AStdClassdef then
215 for nsc in nclassdef.n_superclasses do
216 specobject = false
217 var ntype = nsc.n_type
218 var mtype = resolve_mtype_unchecked(mmodule, mclassdef, ntype, false)
219 if mtype == null then continue # Skip because of error
220 if not mtype isa MClassType then
221 error(ntype, "Error: supertypes cannot be a formal type")
222 return
223 end
224 if not mclass.kind.can_specialize(mtype.mclass.kind) then
225 error(ntype, "Error: {mclass.kind} {mclass} cannot specialize {mtype.mclass.kind} {mtype.mclass}")
226 end
227 supertypes.add mtype
228 #print "new super : {mclass} < {mtype}"
229 if mtype.mclass.kind == extern_kind then specpointer = false
230 end
231 end
232
233 if mclassdef.is_intro and objectclass != null then
234 if mclass.kind == extern_kind and mclass.name != "Pointer" then
235 # it is an extern class, but not a Pointer
236 if specpointer then supertypes.add pointerclass.mclass_type
237 else if specobject and mclass.name != "Object" then
238 # it is a standard class without super class (but is not Object)
239 supertypes.add objectclass.mclass_type
240 end
241 end
242
243 mclassdef.set_supertypes(supertypes)
244 if not supertypes.is_empty then self.toolcontext.info("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
245 end
246
247 # Check the validity of the specialization heirarchy
248 private fun check_supertypes(nmodule: AModule, nclassdef: AClassdef)
249 do
250 var mmodule = nmodule.mmodule.as(not null)
251 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
252 var mclass = nclassdef.mclass.as(not null)
253 var mclassdef = nclassdef.mclassdef.as(not null)
254
255 for s in mclassdef.supertypes do
256 if s.is_subtype(mmodule, mclassdef.bound_mtype, mclassdef.bound_mtype) then
257 error(nclassdef, "Error: Inheritance loop for class {mclass} with type {s}")
258 end
259 end
260 end
261
262 # Build the classes of the module `nmodule`.
263 # REQUIRE: classes of imported modules are already build. (let `phase` do the job)
264 private fun build_classes(nmodule: AModule)
265 do
266 var errcount = toolcontext.error_count
267 # Force building recursively
268 if nmodule.build_classes_is_done then return
269 nmodule.build_classes_is_done = true
270 var mmodule = nmodule.mmodule.as(not null)
271 for imp in mmodule.in_importation.direct_greaters do
272
273 if not mmodule2nmodule.has_key(imp) then continue
274 build_classes(mmodule2nmodule[imp])
275 end
276
277 if errcount != toolcontext.error_count then return
278
279 # Create all classes
280 for nclassdef in nmodule.n_classdefs do
281 self.build_a_mclass(nmodule, nclassdef)
282 end
283
284 if errcount != toolcontext.error_count then return
285
286 # Create all classdefs
287 for nclassdef in nmodule.n_classdefs do
288 self.build_a_mclassdef(nmodule, nclassdef)
289 end
290
291 if errcount != toolcontext.error_count then return
292
293 # Create inheritance on all classdefs
294 for nclassdef in nmodule.n_classdefs do
295 self.collect_a_mclassdef_inheritance(nmodule, nclassdef)
296 end
297
298 if errcount != toolcontext.error_count then return
299
300 # Create the mclassdef hierarchy
301 for mclassdef in mmodule.mclassdefs do
302 mclassdef.add_in_hierarchy
303 end
304
305 if errcount != toolcontext.error_count then return
306
307 # Check inheritance
308 for nclassdef in nmodule.n_classdefs do
309 self.check_supertypes(nmodule, nclassdef)
310 end
311
312 if errcount != toolcontext.error_count then return
313
314 # Check unchecked ntypes
315 for nclassdef in nmodule.n_classdefs do
316 if nclassdef isa AStdClassdef then
317 var mclassdef = nclassdef.mclassdef
318 # check bound of formal parameter
319 for nfd in nclassdef.n_formaldefs do
320 var nfdt = nfd.n_type
321 if nfdt != null and nfdt.mtype != null then
322 var bound = resolve_mtype(mmodule, mclassdef, nfdt)
323 if bound == null then return # Forward error
324 end
325 end
326 # check declared super types
327 for nsc in nclassdef.n_superclasses do
328 var ntype = nsc.n_type
329 if ntype.mtype != null then
330 var mtype = resolve_mtype(mmodule, mclassdef, ntype)
331 if mtype == null then return # Forward error
332 end
333 end
334 end
335 end
336
337 if errcount != toolcontext.error_count then return
338
339 # Check clash of ancestors
340 for nclassdef in nmodule.n_classdefs do
341 var mclassdef = nclassdef.mclassdef.as(not null)
342 var superclasses = new HashMap[MClass, MClassType]
343 for scd in mclassdef.in_hierarchy.greaters do
344 for st in scd.supertypes do
345 if not superclasses.has_key(st.mclass) then
346 superclasses[st.mclass] = st
347 else if superclasses[st.mclass] != st then
348 var st1 = superclasses[st.mclass].resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
349 var st2 = st.resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
350 if st1 != st2 then
351 error(nclassdef, "Error: Incompatibles ancestors for {mclassdef.mclass}: {st1}, {st2}")
352 end
353 end
354 end
355 end
356 end
357
358 if errcount != toolcontext.error_count then return
359
360 # TODO: Check that the super-class is not intrusive
361
362 # Check that the superclasses are not already known (by transitivity)
363 for nclassdef in nmodule.n_classdefs do
364 if not nclassdef isa AStdClassdef then continue
365 var mclassdef = nclassdef.mclassdef.as(not null)
366
367 # Get the direct superclasses
368 # Since we are a mclassdef, just look at the mclassdef hierarchy
369 var parents = new Array[MClass]
370 for sup in mclassdef.in_hierarchy.direct_greaters do
371 parents.add(sup.mclass)
372 end
373
374 # Used to track duplicates of superclasses
375 var seen_parents = new ArrayMap[MClass, AType]
376
377 # The Object class
378 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
379
380 # Check each declared superclass to see if it belong to the direct superclass
381 for nsc in nclassdef.n_superclasses do
382 var ntype = nsc.n_type
383 var mtype = ntype.mtype
384 if mtype == null then continue
385 assert mtype isa MClassType
386 var sc = mtype.mclass
387 if not parents.has(sc) or sc == objectclass then
388 # Skip the warning on generated code
389 if ntype.location.file != null and not ntype.location.file.filename.is_empty then
390 warning(ntype, "useless-superclass", "Warning: superfluous super-class {mtype} in class {mclassdef.mclass}.")
391 end
392 else if not seen_parents.has_key(sc) then
393 seen_parents[sc] = ntype
394 else
395 warning(ntype, "useless-superclass", "Warning: duplicated super-class {mtype} in class {mclassdef.mclass}.")
396 end
397 end
398 end
399 end
400
401 # Register the nclassdef associated to each mclassdef
402 # FIXME: why not refine the `MClassDef` class with a nullable attribute?
403 var mclassdef2nclassdef: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
404
405 # Return the static type associated to the node `ntype`.
406 # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
407 # In case of problem, an error is displayed on `ntype` and null is returned.
408 # FIXME: the name "resolve_mtype" is awful
409 fun resolve_mtype_unchecked(mmodule: MModule, mclassdef: nullable MClassDef, ntype: AType, with_virtual: Bool): nullable MType
410 do
411 var name = ntype.n_id.text
412 var res: MType
413
414 # Check virtual type
415 if mclassdef != null and with_virtual then
416 var prop = try_get_mproperty_by_name(ntype, mclassdef, name).as(nullable MVirtualTypeProp)
417 if prop != null then
418 if not ntype.n_types.is_empty then
419 error(ntype, "Type error: formal type {name} cannot have formal parameters.")
420 end
421 res = prop.mvirtualtype
422 if ntype.n_kwnullable != null then res = res.as_nullable
423 ntype.mtype = res
424 return res
425 end
426 end
427
428 # Check parameter type
429 if mclassdef != null and mclassdef.parameter_names.has(name) then
430 if not ntype.n_types.is_empty then
431 error(ntype, "Type error: formal type {name} cannot have formal parameters.")
432 end
433 for i in [0..mclassdef.parameter_names.length[ do
434 if mclassdef.parameter_names[i] == name then
435 res = mclassdef.mclass.mclass_type.arguments[i]
436 if ntype.n_kwnullable != null then res = res.as_nullable
437 ntype.mtype = res
438 return res
439 end
440 end
441 abort
442 end
443
444 # Check class
445 var mclass = try_get_mclass_by_name(ntype, mmodule, name)
446 if mclass != null then
447 var arity = ntype.n_types.length
448 if arity != mclass.arity then
449 if arity == 0 then
450 error(ntype, "Type error: '{name}' is a generic class.")
451 else if mclass.arity == 0 then
452 error(ntype, "Type error: '{name}' is not a generic class.")
453 else
454 error(ntype, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
455 end
456 return null
457 end
458 if arity == 0 then
459 res = mclass.mclass_type
460 if ntype.n_kwnullable != null then res = res.as_nullable
461 ntype.mtype = res
462 return res
463 else
464 var mtypes = new Array[MType]
465 for nt in ntype.n_types do
466 var mt = resolve_mtype_unchecked(mmodule, mclassdef, nt, with_virtual)
467 if mt == null then return null # Forward error
468 mtypes.add(mt)
469 end
470 res = mclass.get_mtype(mtypes)
471 if ntype.n_kwnullable != null then res = res.as_nullable
472 ntype.mtype = res
473 return res
474 end
475 end
476
477 # If everything fail, then give up :(
478 error(ntype, "Type error: class {name} not found in module {mmodule}.")
479 return null
480 end
481
482 # Return the static type associated to the node `ntype`.
483 # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
484 # In case of problem, an error is displayed on `ntype` and null is returned.
485 # FIXME: the name "resolve_mtype" is awful
486 fun resolve_mtype(mmodule: MModule, mclassdef: nullable MClassDef, ntype: AType): nullable MType
487 do
488 var mtype = ntype.mtype
489 if mtype == null then mtype = resolve_mtype_unchecked(mmodule, mclassdef, ntype, true)
490 if mtype == null then return null # Forward error
491
492 if ntype.checked_mtype then return mtype
493 if mtype isa MGenericType then
494 var mclass = mtype.mclass
495 for i in [0..mclass.arity[ do
496 var bound = mclass.intro.bound_mtype.arguments[i]
497 var nt = ntype.n_types[i]
498 var mt = resolve_mtype(mmodule, mclassdef, nt)
499 if mt == null then return null # forward error
500 var anchor
501 if mclassdef != null then anchor = mclassdef.bound_mtype else anchor = null
502 if not mt.is_subtype(mmodule, anchor, bound) then
503 error(nt, "Type error: expected {bound}, got {mt}")
504 return null
505 end
506 end
507 end
508 ntype.checked_mtype = true
509 return mtype
510 end
511
512 end
513
514 redef class AModule
515 # Flag that indicate if the class building is already completed
516 var build_classes_is_done: Bool = false
517 # What is the AClassdef associated to a `MClass`?
518 # Used to check multiple definition of a class.
519 var mclass2nclassdef: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
520 end
521
522 redef class AClassdef
523 # The associated MClass once build by a `ModelBuilder`
524 var mclass: nullable MClass
525 # The associated MClassDef once build by a `ModelBuilder`
526 var mclassdef: nullable MClassDef
527 # All (self and other) definitions for the same mclassdef
528 var all_defs: nullable Array[AClassdef]
529 end
530
531 redef class AClasskind
532 # The class kind associated with the AST node class
533 private fun mkind: MClassKind is abstract
534 end
535 redef class AConcreteClasskind
536 redef fun mkind do return concrete_kind
537 end
538 redef class AAbstractClasskind
539 redef fun mkind do return abstract_kind
540 end
541 redef class AInterfaceClasskind
542 redef fun mkind do return interface_kind
543 end
544 redef class AEnumClasskind
545 redef fun mkind do return enum_kind
546 end
547 redef class AExternClasskind
548 redef fun mkind do return extern_kind
549 end
550
551 redef class AFormaldef
552 # The associated parameter type
553 var mtype: nullable MParameterType = null
554
555 # The associated bound
556 var bound: nullable MType = null
557 end
558
559 redef class AType
560 # The mtype associated to the node
561 var mtype: nullable MType = null
562
563 # Is the mtype a valid one?
564 var checked_mtype: Bool = false
565 end