modelize_class: Link `subset_kind` with its corresponding production
[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 objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
153 var mclass = nclassdef.mclass
154 if mclass == null then return # Skip error
155
156 # In case of non-standard AClassdef, try to attach to an already existing mclassdef
157 var other_nclassdef = nmodule.mclass2nclassdef[mclass]
158 if other_nclassdef != nclassdef then
159 assert not nclassdef isa AStdClassdef
160 nclassdef.mclassdef = other_nclassdef.mclassdef
161 return
162 end
163
164 var bounds = new Array[MType]
165 if nclassdef isa AStdClassdef and mclass.arity > 0 then
166 # Revolve bound for formal parameters
167 for i in [0..mclass.arity[ do
168 if nclassdef.n_formaldefs.is_empty then
169 # Inherit the bound
170 var bound = mclass.intro.bound_mtype.arguments[i]
171 bounds.add(bound)
172 continue
173 end
174
175 var nfd = nclassdef.n_formaldefs[i]
176 var pname = mclass.mparameters[i].name
177 if nfd.n_id.text != pname then
178 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}`.")
179 end
180 var nfdt = nfd.n_type
181 if nfdt != null then
182 var bound = resolve_mtype3_unchecked(mmodule, null, null, nfdt, false)
183 if bound == null then return # Forward error
184 if bound.need_anchor then
185 # No F-bounds!
186 error(nfd, "Error: formal parameter type `{pname}` bounded with a formal parameter type.")
187 else
188 bounds.add(bound)
189 nfd.bound = bound
190 end
191 else if mclass.mclassdefs.is_empty then
192 if objectclass == null then
193 error(nfd, "Error: formal parameter type `{pname}` unbounded but no `Object` class exists.")
194 return
195 end
196 # No bound, then implicitely bound by nullable Object
197 var bound = objectclass.mclass_type.as_nullable
198 bounds.add(bound)
199 nfd.bound = bound
200 else
201 # Inherit the bound
202 var bound = mclass.intro.bound_mtype.arguments[i]
203 bounds.add(bound)
204 nfd.bound = bound
205 end
206 end
207 end
208
209 var bound_mtype = mclass.get_mtype(bounds)
210 var mclassdef = new MClassDef(mmodule, bound_mtype, nclassdef.location)
211 nclassdef.mclassdef = mclassdef
212 self.mclassdef2nclassdef[mclassdef] = nclassdef
213
214 if nclassdef isa AStdClassdef then
215 var ndoc = nclassdef.n_doc
216 if ndoc != null then
217 var mdoc = ndoc.to_mdoc
218 mclassdef.mdoc = mdoc
219 mdoc.original_mentity = mclassdef
220 else if mclassdef.is_intro and mclass.visibility >= public_visibility then
221 advice(nclassdef, "missing-doc", "Documentation warning: Undocumented public class `{mclass}`")
222 end
223 end
224
225 if mclassdef.is_intro then
226 self.toolcontext.info("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
227 else
228 self.toolcontext.info("{mclassdef} refines {mclass.kind} {mclass.full_name}", 3)
229 end
230 end
231
232 # Visit the AST and set the super-types of the `MClassDef` objects
233 private fun collect_a_mclassdef_inheritance(nmodule: AModule, nclassdef: AClassdef)
234 do
235 var mmodule = nmodule.mmodule
236 if mmodule == null then return
237 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
238 var pointerclass = try_get_mclass_by_name(nmodule, mmodule, "Pointer")
239 var mclass = nclassdef.mclass
240 if mclass == null then return
241 var mclassdef = nclassdef.mclassdef
242 if mclassdef == null then return
243
244 # Do we need to specify Object as a super class?
245 var specobject = true
246
247 # Do we need to specify Pointer as a super class? (is only valid
248 # if `nclassdef` is an extern class)
249 var specpointer = true
250
251 var supertypes = new Array[MClassType]
252 if nclassdef isa AStdClassdef then
253 for nsc in nclassdef.n_superclasses do
254 specobject = false
255 var ntype = nsc.n_type
256 var mtype = resolve_mtype_unchecked(mclassdef, ntype, false)
257 if mtype == null then continue # Skip because of error
258 if not mtype isa MClassType then
259 error(ntype, "Error: supertypes cannot be a formal type.")
260 return
261 end
262 if not mclass.kind.can_specialize(mtype.mclass.kind) then
263 error(ntype, "Error: {mclass.kind} `{mclass}` cannot specialize {mtype.mclass.kind} `{mtype.mclass}`.")
264 end
265 supertypes.add mtype
266 #print "new super : {mclass} < {mtype}"
267 if mtype.mclass.kind == extern_kind then specpointer = false
268 end
269 end
270
271 if mclassdef.is_intro and objectclass != null then
272 if mclass.kind == extern_kind and mclass.name != "Pointer" then
273 # it is an extern class, but not a Pointer
274 if pointerclass == null then
275 error(nclassdef, "Error: `Pointer` must be defined first.")
276 return
277 end
278 if specpointer then supertypes.add pointerclass.mclass_type
279 else if specobject then
280 if mclass.name != "Object" then
281 # it is a standard class without super class (but is not Object)
282 supertypes.add objectclass.mclass_type
283 else if mclass.kind != interface_kind then
284 error(nclassdef, "Error: `Object` must be an {interface_kind}.")
285 return
286 end
287 end
288 end
289
290 mclassdef.set_supertypes(supertypes)
291 if not supertypes.is_empty then self.toolcontext.info("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
292 end
293
294 # Check the validity of the specialization heirarchy
295 private fun check_supertypes(nmodule: AModule, nclassdef: AClassdef)
296 do
297 var mmodule = nmodule.mmodule
298 if mmodule == null then return
299 var mclass = nclassdef.mclass
300 if mclass == null then return
301 var mclassdef = nclassdef.mclassdef
302 if mclassdef == null then return
303
304 for s in mclassdef.supertypes do
305 if s.is_subtype(mmodule, mclassdef.bound_mtype, mclassdef.bound_mtype) then
306 error(nclassdef, "Error: inheritance loop for class `{mclass}` with type `{s}`.")
307 end
308 end
309 end
310
311 # Build the classes of the module `nmodule`.
312 private fun build_classes(nmodule: AModule)
313 do
314 # Force building recursively
315 if nmodule.build_classes_is_done then return
316 nmodule.build_classes_is_done = true
317 var mmodule = nmodule.mmodule
318 if mmodule == null then return
319 for imp in mmodule.in_importation.direct_greaters do
320 var nimp = mmodule2node(imp)
321 if nimp != null then build_classes(nimp)
322 end
323
324 # Create all classes
325 # process AStdClassdef before so that non-AStdClassdef classes can be attached to existing ones, if any
326 for nclassdef in nmodule.n_classdefs do
327 if not nclassdef isa AStdClassdef then continue
328 self.build_a_mclass(nmodule, nclassdef)
329 end
330 for nclassdef in nmodule.n_classdefs do
331 if nclassdef isa AStdClassdef then continue
332 self.build_a_mclass(nmodule, nclassdef)
333 end
334
335 # Create all classdefs
336 for nclassdef in nmodule.n_classdefs do
337 if not nclassdef isa AStdClassdef then continue
338 self.build_a_mclassdef(nmodule, nclassdef)
339 end
340 for nclassdef in nmodule.n_classdefs do
341 if nclassdef isa AStdClassdef then continue
342 self.build_a_mclassdef(nmodule, nclassdef)
343 end
344
345 # Create inheritance on all classdefs
346 for nclassdef in nmodule.n_classdefs do
347 self.collect_a_mclassdef_inheritance(nmodule, nclassdef)
348 end
349
350 # Create the mclassdef hierarchy
351 for mclassdef in mmodule.mclassdefs do
352 mclassdef.add_in_hierarchy
353 end
354
355 # Check inheritance
356 for nclassdef in nmodule.n_classdefs do
357 self.check_supertypes(nmodule, nclassdef)
358 end
359
360 # Check unchecked ntypes
361 for nclassdef in nmodule.n_classdefs do
362 if nclassdef isa AStdClassdef then
363 var mclassdef = nclassdef.mclassdef
364 var mclass
365 var anchor
366 if mclassdef == null then
367 mclass = null
368 anchor = null
369 else
370 mclass = mclassdef.mclass
371 anchor = mclassdef.bound_mtype
372 end
373
374 # check bound of formal parameter
375 for nfd in nclassdef.n_formaldefs do
376 var nfdt = nfd.n_type
377 if nfdt != null and nfdt.mtype != null then
378 var bound = resolve_mtype3(mmodule, mclass, anchor, nfdt)
379 if bound == null then return # Forward error
380 end
381 end
382 # check declared super types
383 for nsc in nclassdef.n_superclasses do
384 var ntype = nsc.n_type
385 if ntype.mtype != null then
386 var mtype = resolve_mtype3(mmodule, mclass, anchor, ntype)
387 if mtype == null then return # Forward error
388 end
389 end
390 end
391 end
392
393 # Check clash of ancestors
394 for nclassdef in nmodule.n_classdefs do
395 var mclassdef = nclassdef.mclassdef
396 if mclassdef == null then continue
397 var superclasses = new HashMap[MClass, MClassType]
398 for scd in mclassdef.in_hierarchy.greaters do
399 for st in scd.supertypes do
400 if not superclasses.has_key(st.mclass) then
401 superclasses[st.mclass] = st
402 else if superclasses[st.mclass] != st then
403 var st1 = superclasses[st.mclass].resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
404 var st2 = st.resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
405 if st1 != st2 then
406 error(nclassdef, "Error: incompatible ancestors for `{mclassdef.mclass}`; conflict: `{st1}` and `{st2}`")
407 end
408 end
409 end
410 end
411 end
412
413 # TODO: Check that the super-class is not intrusive
414
415 # Check that the superclasses are not already known (by transitivity)
416 for nclassdef in nmodule.n_classdefs do
417 if not nclassdef isa AStdClassdef or nclassdef.is_broken then continue
418 var mclassdef = nclassdef.mclassdef
419 if mclassdef == null then continue
420
421 # Get the direct superclasses
422 # Since we are a mclassdef, just look at the mclassdef hierarchy
423 var parents = new Array[MClass]
424 for sup in mclassdef.in_hierarchy.direct_greaters do
425 parents.add(sup.mclass)
426 end
427
428 # Used to track duplicates of superclasses
429 var seen_parents = new ArrayMap[MClass, AType]
430
431 # The Object class
432 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
433
434 # Check each declared superclass to see if it belong to the direct superclass
435 for nsc in nclassdef.n_superclasses do
436 var ntype = nsc.n_type
437 var mtype = ntype.mtype
438 if mtype == null then continue
439 assert mtype isa MClassType
440 var sc = mtype.mclass
441 if not parents.has(sc) or sc == objectclass then
442 # Skip the warning on generated code
443 if ntype.location.file != null and not ntype.location.file.filename.is_empty then
444 warning(ntype, "useless-superclass", "Warning: superfluous super-class `{mtype}` in class `{mclassdef.mclass}`.")
445 end
446 else if not seen_parents.has_key(sc) then
447 seen_parents[sc] = ntype
448 else
449 warning(ntype, "useless-superclass", "Warning: duplicated super-class `{mtype}` in class `{mclassdef.mclass}`.")
450 end
451 end
452 end
453 end
454
455 # Registration of the nclassdef associated to each mclassdef
456 private var mclassdef2nclassdef = new HashMap[MClassDef, AClassdef]
457
458 # Retrieve the associated AST node of a mclassdef.
459 #
460 # This method is used to associate model entity with syntactic entities.
461 # If the class definition is not associated with a node, returns `null`.
462 fun mclassdef2node(mclassdef: MClassDef): nullable AClassdef do
463 return mclassdef2nclassdef.get_or_null(mclassdef)
464 end
465 end
466
467 redef class AModule
468 # Flag that indicate if the class building is already completed
469 var build_classes_is_done: Bool = false
470 # What is the AClassdef associated to a `MClass`?
471 # Used to check multiple definition of a class.
472 var mclass2nclassdef: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
473 end
474
475 redef class AClassdef
476 # The associated MClass once build by a `ModelBuilder`
477 var mclass: nullable MClass
478 # The associated MClassDef once build by a `ModelBuilder`
479 var mclassdef: nullable MClassDef
480 # All (self and other) definitions for the same mclassdef
481 var all_defs: nullable Array[AClassdef]
482 end
483
484 redef class AClasskind
485 # The class kind associated with the AST node class
486 private fun mkind: MClassKind is abstract
487 end
488 redef class AConcreteClasskind
489 redef fun mkind do return concrete_kind
490 end
491 redef class AAbstractClasskind
492 redef fun mkind do return abstract_kind
493 end
494 redef class AInterfaceClasskind
495 redef fun mkind do return interface_kind
496 end
497 redef class AEnumClasskind
498 redef fun mkind do return enum_kind
499 end
500 redef class AExternClasskind
501 redef fun mkind do return extern_kind
502 end
503 redef class ASubsetClasskind
504 redef fun mkind do return subset_kind
505 end
506
507 redef class AFormaldef
508 # The associated parameter type
509 var mtype: nullable MParameterType = null
510
511 # The associated bound
512 var bound: nullable MType = null
513 end