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