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