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