1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Analysis and verification of class definitions to instantiate model element
22 redef class ToolContext
23 # Run `AModule::build_classes` on each module
24 var modelize_class_phase
: Phase = new ModelizeClassPhase(self, null)
27 private class ModelizeClassPhase
30 redef fun process_nmodule
(nmodule
)
32 toolcontext
.modelbuilder
.build_classes
(nmodule
)
36 redef class ModelBuilder
37 # Visit the AST and create the `MClass` objects
38 private fun build_a_mclass
(nmodule
: AModule, nclassdef
: AClassdef)
40 var mmodule
= nmodule
.mmodule
.as(not null)
43 var nkind
: nullable AClasskind
45 var nvisibility
: nullable AVisibility
46 var mvisibility
: nullable MVisibility
48 var names
= new Array[String]
49 if nclassdef
isa AStdClassdef then
50 name
= nclassdef
.n_id
.text
51 nkind
= nclassdef
.n_classkind
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.")
59 else if mvisibility
== intrude_visibility
then
60 error
(nvisibility
, "Error: intrude is not a legal visibility for classes.")
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")
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}")
78 else if nclassdef
isa ATopClassdef then
81 mkind
= interface_kind
83 mvisibility
= public_visibility
84 else if nclassdef
isa AMainClassdef then
89 mvisibility
= public_visibility
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.")
100 mclass
= new MClass(mmodule
, name
, names
, mkind
, mvisibility
)
101 #print "new class {mclass}"
102 else if nclassdef
isa AStdClassdef and nmodule
.mclass2nclassdef
.has_key
(mclass
) then
103 error
(nclassdef
, "Error: A class {name} is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
105 else if nclassdef
isa AStdClassdef and nclassdef
.n_kwredef
== null then
106 error
(nclassdef
, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
108 else if arity
!= 0 and mclass
.arity
!= arity
then
109 error
(nclassdef
, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
111 else if nkind
!= null and mkind
!= concrete_kind
and mclass
.kind
!= mkind
then
112 error
(nkind
, "Error: refinement changed the kind from a {mclass.kind} to a {mkind}")
113 else if nvisibility
!= null and mvisibility
!= public_visibility
and mclass
.visibility
!= mvisibility
then
114 error
(nvisibility
, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
116 nclassdef
.mclass
= mclass
117 if not nmodule
.mclass2nclassdef
.has_key
(mclass
) then
118 nmodule
.mclass2nclassdef
[mclass
] = nclassdef
119 nclassdef
.all_defs
= [nclassdef
]
121 nmodule
.mclass2nclassdef
[mclass
].all_defs
.add
(nclassdef
)
125 # Visit the AST and create the `MClassDef` objects
126 private fun build_a_mclassdef
(nmodule
: AModule, nclassdef
: AClassdef)
128 var mmodule
= nmodule
.mmodule
.as(not null)
129 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
130 var mclass
= nclassdef
.mclass
131 if mclass
== null then return # Skip error
133 # In case of non-standard AClassdef, try to attach to an already existing mclassdef
134 var other_nclassdef
= nmodule
.mclass2nclassdef
[mclass
]
135 if other_nclassdef
!= nclassdef
then
136 assert not nclassdef
isa AStdClassdef
137 nclassdef
.mclassdef
= other_nclassdef
.mclassdef
141 var bounds
= new Array[MType]
142 if nclassdef
isa AStdClassdef and mclass
.arity
> 0 then
143 # Revolve bound for formal parameters
144 for i
in [0..mclass
.arity
[ do
145 if nclassdef
.n_formaldefs
.is_empty
then
147 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
152 var nfd
= nclassdef
.n_formaldefs
[i
]
153 var pname
= mclass
.mparameters
[i
].name
154 if nfd
.n_id
.text
!= pname
then
155 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}`.")
157 var nfdt
= nfd
.n_type
159 var bound
= resolve_mtype_unchecked
(mmodule
, null, nfdt
, false)
160 if bound
== null then return # Forward error
161 if bound
.need_anchor
then
163 error
(nfd
, "Error: Formal parameter type `{pname}' bounded with a formal parameter type")
168 if bound
isa MClassType and bound
.mclass
.kind
== enum_kind
then
169 warning
(nfdt
, "useless-bound", "Warning: Useless formal parameter type since `{bound}` cannnot have subclasses.")
171 else if mclass
.mclassdefs
.is_empty
then
172 if objectclass
== null then
173 error
(nfd
, "Error: Formal parameter type `{pname}' unbounded but no Object class exist.")
176 # No bound, then implicitely bound by nullable Object
177 var bound
= objectclass
.mclass_type
.as_nullable
182 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
189 var bound_mtype
= mclass
.get_mtype
(bounds
)
190 var mclassdef
= new MClassDef(mmodule
, bound_mtype
, nclassdef
.location
)
191 nclassdef
.mclassdef
= mclassdef
192 self.mclassdef2nclassdef
[mclassdef
] = nclassdef
194 if nclassdef
isa AStdClassdef then
195 var ndoc
= nclassdef
.n_doc
197 var mdoc
= ndoc
.to_mdoc
198 mclassdef
.mdoc
= mdoc
199 mdoc
.original_mentity
= mclassdef
200 else if mclassdef
.is_intro
and mclass
.visibility
>= public_visibility
then
201 advice
(nclassdef
, "missing-doc", "Documentation warning: Undocumented public class `{mclass}`")
205 if mclassdef
.is_intro
then
206 self.toolcontext
.info
("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
208 self.toolcontext
.info
("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
212 # Visit the AST and set the super-types of the `MClassDef` objects
213 private fun collect_a_mclassdef_inheritance
(nmodule
: AModule, nclassdef
: AClassdef)
215 var mmodule
= nmodule
.mmodule
.as(not null)
216 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
217 var pointerclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Pointer")
218 var mclass
= nclassdef
.mclass
.as(not null)
219 var mclassdef
= nclassdef
.mclassdef
.as(not null)
221 # Do we need to specify Object as a super class?
222 var specobject
= true
224 # Do we need to specify Pointer as a super class? (is only valid
225 # if `nclassdef` is an extern class)
226 var specpointer
= true
228 var supertypes
= new Array[MClassType]
229 if nclassdef
isa AStdClassdef then
230 for nsc
in nclassdef
.n_superclasses
do
232 var ntype
= nsc
.n_type
233 var mtype
= resolve_mtype_unchecked
(mmodule
, mclassdef
, ntype
, false)
234 if mtype
== null then continue # Skip because of error
235 if not mtype
isa MClassType then
236 error
(ntype
, "Error: supertypes cannot be a formal type")
239 if not mclass
.kind
.can_specialize
(mtype
.mclass
.kind
) then
240 error
(ntype
, "Error: {mclass.kind} {mclass} cannot specialize {mtype.mclass.kind} {mtype.mclass}")
243 #print "new super : {mclass} < {mtype}"
244 if mtype
.mclass
.kind
== extern_kind
then specpointer
= false
248 if mclassdef
.is_intro
and objectclass
!= null then
249 if mclass
.kind
== extern_kind
and mclass
.name
!= "Pointer" then
250 # it is an extern class, but not a Pointer
251 if specpointer
then supertypes
.add pointerclass
.mclass_type
252 else if specobject
and mclass
.name
!= "Object" then
253 # it is a standard class without super class (but is not Object)
254 supertypes
.add objectclass
.mclass_type
258 mclassdef
.set_supertypes
(supertypes
)
259 if not supertypes
.is_empty
then self.toolcontext
.info
("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
262 # Check the validity of the specialization heirarchy
263 private fun check_supertypes
(nmodule
: AModule, nclassdef
: AClassdef)
265 var mmodule
= nmodule
.mmodule
.as(not null)
266 var mclass
= nclassdef
.mclass
.as(not null)
267 var mclassdef
= nclassdef
.mclassdef
.as(not null)
269 for s
in mclassdef
.supertypes
do
270 if s
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, mclassdef
.bound_mtype
) then
271 error
(nclassdef
, "Error: Inheritance loop for class {mclass} with type {s}")
276 # Build the classes of the module `nmodule`.
277 # REQUIRE: classes of imported modules are already build. (let `phase` do the job)
278 private fun build_classes
(nmodule
: AModule)
280 var errcount
= toolcontext
.error_count
281 # Force building recursively
282 if nmodule
.build_classes_is_done
then return
283 nmodule
.build_classes_is_done
= true
284 var mmodule
= nmodule
.mmodule
.as(not null)
285 for imp
in mmodule
.in_importation
.direct_greaters
do
287 if not mmodule2nmodule
.has_key
(imp
) then continue
288 build_classes
(mmodule2nmodule
[imp
])
291 if errcount
!= toolcontext
.error_count
then return
294 for nclassdef
in nmodule
.n_classdefs
do
295 self.build_a_mclass
(nmodule
, nclassdef
)
298 if errcount
!= toolcontext
.error_count
then return
300 # Create all classdefs
301 for nclassdef
in nmodule
.n_classdefs
do
302 self.build_a_mclassdef
(nmodule
, nclassdef
)
305 if errcount
!= toolcontext
.error_count
then return
307 # Create inheritance on all classdefs
308 for nclassdef
in nmodule
.n_classdefs
do
309 self.collect_a_mclassdef_inheritance
(nmodule
, nclassdef
)
312 if errcount
!= toolcontext
.error_count
then return
314 # Create the mclassdef hierarchy
315 for mclassdef
in mmodule
.mclassdefs
do
316 mclassdef
.add_in_hierarchy
319 if errcount
!= toolcontext
.error_count
then return
322 for nclassdef
in nmodule
.n_classdefs
do
323 self.check_supertypes
(nmodule
, nclassdef
)
326 if errcount
!= toolcontext
.error_count
then return
328 # Check unchecked ntypes
329 for nclassdef
in nmodule
.n_classdefs
do
330 if nclassdef
isa AStdClassdef then
331 var mclassdef
= nclassdef
.mclassdef
332 # check bound of formal parameter
333 for nfd
in nclassdef
.n_formaldefs
do
334 var nfdt
= nfd
.n_type
335 if nfdt
!= null and nfdt
.mtype
!= null then
336 var bound
= resolve_mtype
(mmodule
, mclassdef
, nfdt
)
337 if bound
== null then return # Forward error
340 # check declared super types
341 for nsc
in nclassdef
.n_superclasses
do
342 var ntype
= nsc
.n_type
343 if ntype
.mtype
!= null then
344 var mtype
= resolve_mtype
(mmodule
, mclassdef
, ntype
)
345 if mtype
== null then return # Forward error
351 if errcount
!= toolcontext
.error_count
then return
353 # Check clash of ancestors
354 for nclassdef
in nmodule
.n_classdefs
do
355 var mclassdef
= nclassdef
.mclassdef
.as(not null)
356 var superclasses
= new HashMap[MClass, MClassType]
357 for scd
in mclassdef
.in_hierarchy
.greaters
do
358 for st
in scd
.supertypes
do
359 if not superclasses
.has_key
(st
.mclass
) then
360 superclasses
[st
.mclass
] = st
361 else if superclasses
[st
.mclass
] != st
then
362 var st1
= superclasses
[st
.mclass
].resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
363 var st2
= st
.resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
365 error
(nclassdef
, "Error: Incompatibles ancestors for {mclassdef.mclass}: {st1}, {st2}")
372 if errcount
!= toolcontext
.error_count
then return
374 # TODO: Check that the super-class is not intrusive
376 # Check that the superclasses are not already known (by transitivity)
377 for nclassdef
in nmodule
.n_classdefs
do
378 if not nclassdef
isa AStdClassdef then continue
379 var mclassdef
= nclassdef
.mclassdef
.as(not null)
381 # Get the direct superclasses
382 # Since we are a mclassdef, just look at the mclassdef hierarchy
383 var parents
= new Array[MClass]
384 for sup
in mclassdef
.in_hierarchy
.direct_greaters
do
385 parents
.add
(sup
.mclass
)
388 # Used to track duplicates of superclasses
389 var seen_parents
= new ArrayMap[MClass, AType]
392 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
394 # Check each declared superclass to see if it belong to the direct superclass
395 for nsc
in nclassdef
.n_superclasses
do
396 var ntype
= nsc
.n_type
397 var mtype
= ntype
.mtype
398 if mtype
== null then continue
399 assert mtype
isa MClassType
400 var sc
= mtype
.mclass
401 if not parents
.has
(sc
) or sc
== objectclass
then
402 # Skip the warning on generated code
403 if ntype
.location
.file
!= null and not ntype
.location
.file
.filename
.is_empty
then
404 warning
(ntype
, "useless-superclass", "Warning: superfluous super-class {mtype} in class {mclassdef.mclass}.")
406 else if not seen_parents
.has_key
(sc
) then
407 seen_parents
[sc
] = ntype
409 warning
(ntype
, "useless-superclass", "Warning: duplicated super-class {mtype} in class {mclassdef.mclass}.")
415 # Registration of the nclassdef associated to each mclassdef
416 private var mclassdef2nclassdef
= new HashMap[MClassDef, AClassdef]
418 # Return the static type associated to the node `ntype`.
419 # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
420 # In case of problem, an error is displayed on `ntype` and null is returned.
421 # FIXME: the name "resolve_mtype" is awful
422 fun resolve_mtype_unchecked
(mmodule
: MModule, mclassdef
: nullable MClassDef, ntype
: AType, with_virtual
: Bool): nullable MType
424 var name
= ntype
.n_id
.text
428 if mclassdef
!= null and with_virtual
then
429 var prop
= try_get_mproperty_by_name
(ntype
, mclassdef
, name
).as(nullable MVirtualTypeProp)
431 if not ntype
.n_types
.is_empty
then
432 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
434 res
= prop
.mvirtualtype
435 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
441 # Check parameter type
442 if mclassdef
!= null then
443 for p
in mclassdef
.mclass
.mparameters
do
444 if p
.name
!= name
then continue
446 if not ntype
.n_types
.is_empty
then
447 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
451 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
458 var mclass
= try_get_mclass_by_name
(ntype
, mmodule
, name
)
459 if mclass
!= null then
460 var arity
= ntype
.n_types
.length
461 if arity
!= mclass
.arity
then
463 error
(ntype
, "Type error: '{name}' is a generic class.")
464 else if mclass
.arity
== 0 then
465 error
(ntype
, "Type error: '{name}' is not a generic class.")
467 error
(ntype
, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
472 res
= mclass
.mclass_type
473 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
477 var mtypes
= new Array[MType]
478 for nt
in ntype
.n_types
do
479 var mt
= resolve_mtype_unchecked
(mmodule
, mclassdef
, nt
, with_virtual
)
480 if mt
== null then return null # Forward error
483 res
= mclass
.get_mtype
(mtypes
)
484 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
490 # If everything fail, then give up :(
491 error
(ntype
, "Type error: class {name} not found in module {mmodule}.")
495 # Return the static type associated to the node `ntype`.
496 # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
497 # In case of problem, an error is displayed on `ntype` and null is returned.
498 # FIXME: the name "resolve_mtype" is awful
499 fun resolve_mtype
(mmodule
: MModule, mclassdef
: nullable MClassDef, ntype
: AType): nullable MType
501 var mtype
= ntype
.mtype
502 if mtype
== null then mtype
= resolve_mtype_unchecked
(mmodule
, mclassdef
, ntype
, true)
503 if mtype
== null then return null # Forward error
505 if ntype
.checked_mtype
then return mtype
506 if mtype
isa MGenericType then
507 var mclass
= mtype
.mclass
508 for i
in [0..mclass
.arity
[ do
509 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
510 var nt
= ntype
.n_types
[i
]
511 var mt
= resolve_mtype
(mmodule
, mclassdef
, nt
)
512 if mt
== null then return null # forward error
514 if mclassdef
!= null then anchor
= mclassdef
.bound_mtype
else anchor
= null
515 if not mt
.is_subtype
(mmodule
, anchor
, bound
) then
516 error
(nt
, "Type error: expected {bound}, got {mt}")
521 ntype
.checked_mtype
= true
528 # Flag that indicate if the class building is already completed
529 var build_classes_is_done
: Bool = false
530 # What is the AClassdef associated to a `MClass`?
531 # Used to check multiple definition of a class.
532 var mclass2nclassdef
: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
535 redef class AClassdef
536 # The associated MClass once build by a `ModelBuilder`
537 var mclass
: nullable MClass
538 # The associated MClassDef once build by a `ModelBuilder`
539 var mclassdef
: nullable MClassDef
540 # All (self and other) definitions for the same mclassdef
541 var all_defs
: nullable Array[AClassdef]
544 redef class AClasskind
545 # The class kind associated with the AST node class
546 private fun mkind
: MClassKind is abstract
548 redef class AConcreteClasskind
549 redef fun mkind
do return concrete_kind
551 redef class AAbstractClasskind
552 redef fun mkind
do return abstract_kind
554 redef class AInterfaceClasskind
555 redef fun mkind
do return interface_kind
557 redef class AEnumClasskind
558 redef fun mkind
do return enum_kind
560 redef class AExternClasskind
561 redef fun mkind
do return extern_kind
564 redef class AFormaldef
565 # The associated parameter type
566 var mtype
: nullable MParameterType = null
568 # The associated bound
569 var bound
: nullable MType = null
573 # The mtype associated to the node
574 var mtype
: nullable MType = null
576 # Is the mtype a valid one?
577 var checked_mtype
: Bool = false