Merge remote-tracking branch 'origin/master' into init_auto
[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 if nclassdef isa AStdClassdef and nclassdef.n_kwredef != null then
108 error(nclassdef, "Redef Error: no imported class `{name}` to refine.")
109 return
110 end
111
112 # Check for conflicting class full-names in the package
113 if mmodule.mgroup != null and mvisibility >= protected_visibility then
114 var mclasses = model.get_mclasses_by_name(name)
115 if mclasses != null then for other in mclasses do
116 if other.intro_mmodule.mgroup != null and other.intro_mmodule.mgroup.mpackage == mmodule.mgroup.mpackage then
117 # Skip classes that are buggy
118 if other.try_intro == null then continue
119 warning(nclassdef, "full-name-conflict", "Error: a class named `{other.full_name}` is already defined in module `{other.intro_mmodule}` at {other.intro.location}.")
120 break
121 end
122 end
123 end
124
125 mclass = new MClass(mmodule, name, nclassdef.location, names, mkind, mvisibility)
126 #print "new class {mclass}"
127 else if nclassdef isa AStdClassdef and nmodule.mclass2nclassdef.has_key(mclass) then
128 error(nclassdef, "Error: a class `{name}` is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
129 mclass.is_broken = true
130 return
131 else if nclassdef isa AStdClassdef and nclassdef.n_kwredef == null then
132 error(nclassdef, "Redef Error: `{name}` is an imported class. Add the `redef` keyword to refine it.")
133 mclass.is_broken = true
134 return
135 else if arity != 0 and mclass.arity != arity then
136 error(nclassdef, "Redef Error: expected {mclass.arity} formal parameter(s) for {mclass.signature_to_s}; got {arity}.")
137 mclass.is_broken = true
138 return
139 else if nkind != null and mkind != concrete_kind and mclass.kind != mkind then
140 error(nkind, "Redef Error: refinement changed the kind from `{mclass.kind}` to `{mkind}`.")
141 else if nvisibility != null and mvisibility != public_visibility and mclass.visibility != mvisibility then
142 error(nvisibility, "Redef Error: refinement changed the visibility from `{mclass.visibility}` to `{mvisibility}`")
143 end
144 nclassdef.mclass = mclass
145 if not nmodule.mclass2nclassdef.has_key(mclass) then
146 nmodule.mclass2nclassdef[mclass] = nclassdef
147 nclassdef.all_defs = [nclassdef]
148 else
149 nmodule.mclass2nclassdef[mclass].all_defs.add(nclassdef)
150 end
151 end
152
153 # Visit the AST and create the `MClassDef` objects
154 private fun build_a_mclassdef(nmodule: AModule, nclassdef: AClassdef)
155 do
156 var mmodule = nmodule.mmodule.as(not null)
157 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
158 var mclass = nclassdef.mclass
159 if mclass == null then return # Skip error
160
161 # In case of non-standard AClassdef, try to attach to an already existing mclassdef
162 var other_nclassdef = nmodule.mclass2nclassdef[mclass]
163 if other_nclassdef != nclassdef then
164 assert not nclassdef isa AStdClassdef
165 nclassdef.mclassdef = other_nclassdef.mclassdef
166 return
167 end
168
169 var bounds = new Array[MType]
170 if nclassdef isa AStdClassdef and mclass.arity > 0 then
171 # Revolve bound for formal parameters
172 for i in [0..mclass.arity[ do
173 if nclassdef.n_formaldefs.is_empty then
174 # Inherit the bound
175 var bound = mclass.intro.bound_mtype.arguments[i]
176 bounds.add(bound)
177 continue
178 end
179
180 var nfd = nclassdef.n_formaldefs[i]
181 var pname = mclass.mparameters[i].name
182 if nfd.n_id.text != pname then
183 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}`.")
184 end
185 var nfdt = nfd.n_type
186 if nfdt != null then
187 var bound = resolve_mtype_unchecked(mmodule, null, nfdt, false)
188 if bound == null then return # Forward error
189 if bound.need_anchor then
190 # No F-bounds!
191 error(nfd, "Error: formal parameter type `{pname}` bounded with a formal parameter type.")
192 else
193 bounds.add(bound)
194 nfd.bound = bound
195 end
196 if bound isa MClassType and bound.mclass.kind == enum_kind then
197 warning(nfdt, "useless-bound", "Warning: useless formal parameter type since `{bound}` cannot have subclasses.")
198 end
199 else if mclass.mclassdefs.is_empty then
200 if objectclass == null then
201 error(nfd, "Error: formal parameter type `{pname}` unbounded but no `Object` class exists.")
202 return
203 end
204 # No bound, then implicitely bound by nullable Object
205 var bound = objectclass.mclass_type.as_nullable
206 bounds.add(bound)
207 nfd.bound = bound
208 else
209 # Inherit the bound
210 var bound = mclass.intro.bound_mtype.arguments[i]
211 bounds.add(bound)
212 nfd.bound = bound
213 end
214 end
215 end
216
217 var bound_mtype = mclass.get_mtype(bounds)
218 var mclassdef = new MClassDef(mmodule, bound_mtype, nclassdef.location)
219 nclassdef.mclassdef = mclassdef
220 self.mclassdef2nclassdef[mclassdef] = nclassdef
221
222 if nclassdef isa AStdClassdef then
223 var ndoc = nclassdef.n_doc
224 if ndoc != null then
225 var mdoc = ndoc.to_mdoc
226 mclassdef.mdoc = mdoc
227 mdoc.original_mentity = mclassdef
228 else if mclassdef.is_intro and mclass.visibility >= public_visibility then
229 advice(nclassdef, "missing-doc", "Documentation warning: Undocumented public class `{mclass}`")
230 end
231 end
232
233 if mclassdef.is_intro then
234 self.toolcontext.info("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
235 else
236 self.toolcontext.info("{mclassdef} refines {mclass.kind} {mclass.full_name}", 3)
237 end
238 end
239
240 # Visit the AST and set the super-types of the `MClassDef` objects
241 private fun collect_a_mclassdef_inheritance(nmodule: AModule, nclassdef: AClassdef)
242 do
243 var mmodule = nmodule.mmodule
244 if mmodule == null then return
245 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
246 var pointerclass = try_get_mclass_by_name(nmodule, mmodule, "Pointer")
247 var mclass = nclassdef.mclass
248 if mclass == null then return
249 var mclassdef = nclassdef.mclassdef
250 if mclassdef == null then return
251
252 # Do we need to specify Object as a super class?
253 var specobject = true
254
255 # Do we need to specify Pointer as a super class? (is only valid
256 # if `nclassdef` is an extern class)
257 var specpointer = true
258
259 var supertypes = new Array[MClassType]
260 if nclassdef isa AStdClassdef then
261 for nsc in nclassdef.n_superclasses do
262 specobject = false
263 var ntype = nsc.n_type
264 var mtype = resolve_mtype_unchecked(mmodule, mclassdef, ntype, false)
265 if mtype == null then continue # Skip because of error
266 if not mtype isa MClassType then
267 error(ntype, "Error: supertypes cannot be a formal type.")
268 return
269 end
270 if not mclass.kind.can_specialize(mtype.mclass.kind) then
271 error(ntype, "Error: {mclass.kind} `{mclass}` cannot specialize {mtype.mclass.kind} `{mtype.mclass}`.")
272 end
273 supertypes.add mtype
274 #print "new super : {mclass} < {mtype}"
275 if mtype.mclass.kind == extern_kind then specpointer = false
276 end
277 end
278
279 if mclassdef.is_intro and objectclass != null then
280 if mclass.kind == extern_kind and mclass.name != "Pointer" then
281 # it is an extern class, but not a Pointer
282 if specpointer then supertypes.add pointerclass.mclass_type
283 else if specobject and mclass.name != "Object" then
284 # it is a standard class without super class (but is not Object)
285 supertypes.add objectclass.mclass_type
286 end
287 end
288
289 mclassdef.set_supertypes(supertypes)
290 if not supertypes.is_empty then self.toolcontext.info("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
291 end
292
293 # Check the validity of the specialization heirarchy
294 private fun check_supertypes(nmodule: AModule, nclassdef: AClassdef)
295 do
296 var mmodule = nmodule.mmodule
297 if mmodule == null then return
298 var mclass = nclassdef.mclass
299 if mclass == null then return
300 var mclassdef = nclassdef.mclassdef
301 if mclassdef == null then return
302
303 for s in mclassdef.supertypes do
304 if s.is_subtype(mmodule, mclassdef.bound_mtype, mclassdef.bound_mtype) then
305 error(nclassdef, "Error: inheritance loop for class `{mclass}` with type `{s}`.")
306 end
307 end
308 end
309
310 # Build the classes of the module `nmodule`.
311 # REQUIRE: classes of imported modules are already build. (let `phase` do the job)
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 # 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 # Check clash of ancestors
384 for nclassdef in nmodule.n_classdefs do
385 var mclassdef = nclassdef.mclassdef
386 if mclassdef == null then continue
387 var superclasses = new HashMap[MClass, MClassType]
388 for scd in mclassdef.in_hierarchy.greaters do
389 for st in scd.supertypes do
390 if not superclasses.has_key(st.mclass) then
391 superclasses[st.mclass] = st
392 else if superclasses[st.mclass] != st then
393 var st1 = superclasses[st.mclass].resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
394 var st2 = st.resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
395 if st1 != st2 then
396 error(nclassdef, "Error: incompatible ancestors for `{mclassdef.mclass}`; conflict: `{st1}` and `{st2}`")
397 end
398 end
399 end
400 end
401 end
402
403 # TODO: Check that the super-class is not intrusive
404
405 # Check that the superclasses are not already known (by transitivity)
406 for nclassdef in nmodule.n_classdefs do
407 if not nclassdef isa AStdClassdef or nclassdef.is_broken then continue
408 var mclassdef = nclassdef.mclassdef
409 if mclassdef == null then continue
410
411 # Get the direct superclasses
412 # Since we are a mclassdef, just look at the mclassdef hierarchy
413 var parents = new Array[MClass]
414 for sup in mclassdef.in_hierarchy.direct_greaters do
415 parents.add(sup.mclass)
416 end
417
418 # Used to track duplicates of superclasses
419 var seen_parents = new ArrayMap[MClass, AType]
420
421 # The Object class
422 var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
423
424 # Check each declared superclass to see if it belong to the direct superclass
425 for nsc in nclassdef.n_superclasses do
426 var ntype = nsc.n_type
427 var mtype = ntype.mtype
428 if mtype == null then continue
429 assert mtype isa MClassType
430 var sc = mtype.mclass
431 if not parents.has(sc) or sc == objectclass then
432 # Skip the warning on generated code
433 if ntype.location.file != null and not ntype.location.file.filename.is_empty then
434 warning(ntype, "useless-superclass", "Warning: superfluous super-class `{mtype}` in class `{mclassdef.mclass}`.")
435 end
436 else if not seen_parents.has_key(sc) then
437 seen_parents[sc] = ntype
438 else
439 warning(ntype, "useless-superclass", "Warning: duplicated super-class `{mtype}` in class `{mclassdef.mclass}`.")
440 end
441 end
442 end
443 end
444
445 # Registration of the nclassdef associated to each mclassdef
446 private var mclassdef2nclassdef = new HashMap[MClassDef, AClassdef]
447
448 # Retrieve the associated AST node of a mclassdef.
449 #
450 # This method is used to associate model entity with syntactic entities.
451 # If the class definition is not associated with a node, returns `null`.
452 fun mclassdef2node(mclassdef: MClassDef): nullable AClassdef do
453 return mclassdef2nclassdef.get_or_null(mclassdef)
454 end
455 end
456
457 redef class AModule
458 # Flag that indicate if the class building is already completed
459 var build_classes_is_done: Bool = false
460 # What is the AClassdef associated to a `MClass`?
461 # Used to check multiple definition of a class.
462 var mclass2nclassdef: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
463 end
464
465 redef class AClassdef
466 # The associated MClass once build by a `ModelBuilder`
467 var mclass: nullable MClass
468 # The associated MClassDef once build by a `ModelBuilder`
469 var mclassdef: nullable MClassDef
470 # All (self and other) definitions for the same mclassdef
471 var all_defs: nullable Array[AClassdef]
472 end
473
474 redef class AClasskind
475 # The class kind associated with the AST node class
476 private fun mkind: MClassKind is abstract
477 end
478 redef class AConcreteClasskind
479 redef fun mkind do return concrete_kind
480 end
481 redef class AAbstractClasskind
482 redef fun mkind do return abstract_kind
483 end
484 redef class AInterfaceClasskind
485 redef fun mkind do return interface_kind
486 end
487 redef class AEnumClasskind
488 redef fun mkind do return enum_kind
489 end
490 redef class AExternClasskind
491 redef fun mkind do return extern_kind
492 end
493
494 redef class AFormaldef
495 # The associated parameter type
496 var mtype: nullable MParameterType = null
497
498 # The associated bound
499 var bound: nullable MType = null
500 end