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