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 var modelize_class_phase
: Phase = new ModelizeClassPhase(self, null)
26 private class ModelizeClassPhase
29 redef fun process_nmodule
(nmodule
)
31 toolcontext
.modelbuilder
.build_classes
(nmodule
)
35 redef class ModelBuilder
36 # Visit the AST and create the `MClass` objects
37 private fun build_a_mclass
(nmodule
: AModule, nclassdef
: AClassdef)
39 var mmodule
= nmodule
.mmodule
.as(not null)
42 var nkind
: nullable AClasskind
44 var nvisibility
: nullable AVisibility
45 var mvisibility
: nullable MVisibility
47 if nclassdef
isa AStdClassdef then
48 name
= nclassdef
.n_id
.text
49 nkind
= nclassdef
.n_classkind
51 nvisibility
= nclassdef
.n_visibility
52 mvisibility
= nvisibility
.mvisibility
53 arity
= nclassdef
.n_formaldefs
.length
54 if mvisibility
== protected_visibility
then
55 error
(nvisibility
, "Error: only properties can be protected.")
57 else if mvisibility
== intrude_visibility
then
58 error
(nvisibility
, "Error: intrude is not a legal visibility for classes.")
61 else if nclassdef
isa ATopClassdef then
64 mkind
= interface_kind
66 mvisibility
= public_visibility
67 else if nclassdef
isa AMainClassdef then
72 mvisibility
= public_visibility
77 var mclass
= try_get_mclass_by_name
(nclassdef
, mmodule
, name
)
78 if mclass
== null then
79 if nclassdef
isa AStdClassdef and nclassdef
.n_kwredef
!= null then
80 error
(nclassdef
, "Redef error: No imported class {name} to refine.")
83 mclass
= new MClass(mmodule
, name
, arity
, mkind
, mvisibility
)
84 #print "new class {mclass}"
85 else if nclassdef
isa AStdClassdef and nmodule
.mclass2nclassdef
.has_key
(mclass
) then
86 error
(nclassdef
, "Error: A class {name} is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
88 else if nclassdef
isa AStdClassdef and nclassdef
.n_kwredef
== null then
89 error
(nclassdef
, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
91 else if mclass
.arity
!= arity
then
92 error
(nclassdef
, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
94 else if nkind
!= null and mkind
!= concrete_kind
and mclass
.kind
!= mkind
then
95 error
(nkind
, "Error: refinement changed the kind from a {mclass.kind} to a {mkind}")
96 else if nvisibility
!= null and mvisibility
!= public_visibility
and mclass
.visibility
!= mvisibility
then
97 error
(nvisibility
, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
99 nclassdef
.mclass
= mclass
100 if not nmodule
.mclass2nclassdef
.has_key
(mclass
) then
101 nmodule
.mclass2nclassdef
[mclass
] = nclassdef
102 nclassdef
.all_defs
= [nclassdef
]
104 nmodule
.mclass2nclassdef
[mclass
].all_defs
.add
(nclassdef
)
108 # Visit the AST and create the `MClassDef` objects
109 private fun build_a_mclassdef
(nmodule
: AModule, nclassdef
: AClassdef)
111 var mmodule
= nmodule
.mmodule
.as(not null)
112 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
113 var mclass
= nclassdef
.mclass
114 if mclass
== null then return # Skip error
116 # In case of non-standard AClassdef, try to attach to an already existing mclassdef
117 var other_nclassdef
= nmodule
.mclass2nclassdef
[mclass
]
118 if other_nclassdef
!= nclassdef
then
119 assert not nclassdef
isa AStdClassdef
120 nclassdef
.mclassdef
= other_nclassdef
.mclassdef
124 var names
= new Array[String]
125 var bounds
= new Array[MType]
126 if nclassdef
isa AStdClassdef and mclass
.arity
> 0 then
127 # Collect formal parameter names
128 for i
in [0..mclass
.arity
[ do
129 var nfd
= nclassdef
.n_formaldefs
[i
]
130 var ptname
= nfd
.n_id
.text
131 if names
.has
(ptname
) then
132 error
(nfd
, "Error: A formal parameter type `{ptname}' already exists")
135 for c
in ptname
.chars
do if c
>= 'a' and c
<= 'z' then
136 warning
(nfd
, "Warning: lowercase in the formal parameter type {ptname}")
140 nfd
.mtype
= mclass
.mclass_type
.arguments
[i
].as(MParameterType)
143 # Revolve bound for formal parameter names
144 for i
in [0..mclass
.arity
[ do
145 var nfd
= nclassdef
.n_formaldefs
[i
]
146 var nfdt
= nfd
.n_type
148 var bound
= resolve_mtype_unchecked
(mmodule
, null, nfdt
, false)
149 if bound
== null then return # Forward error
150 if bound
.need_anchor
then
152 error
(nfd
, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
157 if bound
isa MClassType and bound
.mclass
.kind
== enum_kind
then
158 warning
(nfdt
, "Warning: Useless formal parameter type since `{bound}` cannnot have subclasses.")
160 else if mclass
.mclassdefs
.is_empty
then
161 # No bound, then implicitely bound by nullable Object
162 var bound
= objectclass
.mclass_type
.as_nullable
167 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
174 var bound_mtype
= mclass
.get_mtype
(bounds
)
175 var mclassdef
= new MClassDef(mmodule
, bound_mtype
, nclassdef
.location
, names
)
176 nclassdef
.mclassdef
= mclassdef
177 self.mclassdef2nclassdef
[mclassdef
] = nclassdef
179 if nclassdef
isa AStdClassdef then
180 var ndoc
= nclassdef
.n_doc
182 var mdoc
= ndoc
.to_mdoc
183 mclassdef
.mdoc
= mdoc
184 mdoc
.original_mentity
= mclassdef
188 if mclassdef
.is_intro
then
189 self.toolcontext
.info
("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
191 self.toolcontext
.info
("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
195 # Visit the AST and set the super-types of the `MClassDef` objects
196 private fun collect_a_mclassdef_inheritance
(nmodule
: AModule, nclassdef
: AClassdef)
198 var mmodule
= nmodule
.mmodule
.as(not null)
199 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
200 var pointerclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Pointer")
201 var mclass
= nclassdef
.mclass
.as(not null)
202 var mclassdef
= nclassdef
.mclassdef
.as(not null)
204 # Do we need to specify Object as a super class?
205 var specobject
= true
207 # Do we need to specify Pointer as a super class? (is only valid
208 # if `nclassdef` is an extern class)
209 var specpointer
= true
211 var supertypes
= new Array[MClassType]
212 if nclassdef
isa AStdClassdef then
213 for nsc
in nclassdef
.n_superclasses
do
215 var ntype
= nsc
.n_type
216 var mtype
= resolve_mtype_unchecked
(mmodule
, mclassdef
, ntype
, false)
217 if mtype
== null then continue # Skip because of error
218 if not mtype
isa MClassType then
219 error
(ntype
, "Error: supertypes cannot be a formal type")
222 if not mclass
.kind
.can_specialize
(mtype
.mclass
.kind
) then
223 error
(ntype
, "Error: {mclass.kind} {mclass} cannot specialize {mtype.mclass.kind} {mtype.mclass}")
226 #print "new super : {mclass} < {mtype}"
227 if mtype
.mclass
.kind
== extern_kind
then specpointer
= false
231 if mclassdef
.is_intro
and objectclass
!= null then
232 if mclass
.kind
== extern_kind
and mclass
.name
!= "Pointer" then
233 # it is an extern class, but not a Pointer
234 if specpointer
then supertypes
.add pointerclass
.mclass_type
235 else if specobject
and mclass
.name
!= "Object" then
236 # it is a standard class without super class (but is not Object)
237 supertypes
.add objectclass
.mclass_type
241 mclassdef
.set_supertypes
(supertypes
)
242 if not supertypes
.is_empty
then self.toolcontext
.info
("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
245 # Check the validity of the specialization heirarchy
246 private fun check_supertypes
(nmodule
: AModule, nclassdef
: AClassdef)
248 var mmodule
= nmodule
.mmodule
.as(not null)
249 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
250 var mclass
= nclassdef
.mclass
.as(not null)
251 var mclassdef
= nclassdef
.mclassdef
.as(not null)
253 for s
in mclassdef
.supertypes
do
254 if s
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, mclassdef
.bound_mtype
) then
255 error
(nclassdef
, "Error: Inheritance loop for class {mclass} with type {s}")
260 # Build the classes of the module `nmodule`.
261 # REQUIRE: classes of imported modules are already build. (let `phase` do the job)
262 private fun build_classes
(nmodule
: AModule)
264 var errcount
= toolcontext
.error_count
265 # Force building recursively
266 if nmodule
.build_classes_is_done
then return
267 nmodule
.build_classes_is_done
= true
268 var mmodule
= nmodule
.mmodule
.as(not null)
269 for imp
in mmodule
.in_importation
.direct_greaters
do
271 if not mmodule2nmodule
.has_key
(imp
) then continue
272 build_classes
(mmodule2nmodule
[imp
])
275 if errcount
!= toolcontext
.error_count
then return
278 for nclassdef
in nmodule
.n_classdefs
do
279 self.build_a_mclass
(nmodule
, nclassdef
)
282 if errcount
!= toolcontext
.error_count
then return
284 # Create all classdefs
285 for nclassdef
in nmodule
.n_classdefs
do
286 self.build_a_mclassdef
(nmodule
, nclassdef
)
289 if errcount
!= toolcontext
.error_count
then return
291 # Create inheritance on all classdefs
292 for nclassdef
in nmodule
.n_classdefs
do
293 self.collect_a_mclassdef_inheritance
(nmodule
, nclassdef
)
296 if errcount
!= toolcontext
.error_count
then return
298 # Create the mclassdef hierarchy
299 for mclassdef
in mmodule
.mclassdefs
do
300 mclassdef
.add_in_hierarchy
303 if errcount
!= toolcontext
.error_count
then return
306 for nclassdef
in nmodule
.n_classdefs
do
307 self.check_supertypes
(nmodule
, nclassdef
)
310 if errcount
!= toolcontext
.error_count
then return
312 # Check unchecked ntypes
313 for nclassdef
in nmodule
.n_classdefs
do
314 if nclassdef
isa AStdClassdef then
315 var mclassdef
= nclassdef
.mclassdef
316 # check bound of formal parameter
317 for nfd
in nclassdef
.n_formaldefs
do
318 var nfdt
= nfd
.n_type
319 if nfdt
!= null and nfdt
.mtype
!= null then
320 var bound
= resolve_mtype
(mmodule
, mclassdef
, nfdt
)
321 if bound
== null then return # Forward error
324 # check declared super types
325 for nsc
in nclassdef
.n_superclasses
do
326 var ntype
= nsc
.n_type
327 if ntype
.mtype
!= null then
328 var mtype
= resolve_mtype
(mmodule
, mclassdef
, ntype
)
329 if mtype
== null then return # Forward error
335 if errcount
!= toolcontext
.error_count
then return
337 # Check clash of ancestors
338 for nclassdef
in nmodule
.n_classdefs
do
339 var mclassdef
= nclassdef
.mclassdef
.as(not null)
340 var superclasses
= new HashMap[MClass, MClassType]
341 for scd
in mclassdef
.in_hierarchy
.greaters
do
342 for st
in scd
.supertypes
do
343 if not superclasses
.has_key
(st
.mclass
) then
344 superclasses
[st
.mclass
] = st
345 else if superclasses
[st
.mclass
] != st
then
346 var st1
= superclasses
[st
.mclass
].resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
347 var st2
= st
.resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
349 error
(nclassdef
, "Error: Incompatibles ancestors for {mclassdef.mclass}: {st1}, {st2}")
356 if errcount
!= toolcontext
.error_count
then return
358 # TODO: Check that the super-class is not intrusive
360 # Check that the superclasses are not already known (by transitivity)
361 for nclassdef
in nmodule
.n_classdefs
do
362 if not nclassdef
isa AStdClassdef then continue
363 var mclassdef
= nclassdef
.mclassdef
.as(not null)
365 # Get the direct superclasses
366 # Since we are a mclassdef, just look at the mclassdef hierarchy
367 var parents
= new Array[MClass]
368 for sup
in mclassdef
.in_hierarchy
.direct_greaters
do
369 parents
.add
(sup
.mclass
)
372 # Used to track duplicates of superclasses
373 var seen_parents
= new ArrayMap[MClass, AType]
376 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
378 # Check each declared superclass to see if it belong to the direct superclass
379 for nsc
in nclassdef
.n_superclasses
do
380 var ntype
= nsc
.n_type
381 var mtype
= ntype
.mtype
382 if mtype
== null then continue
383 assert mtype
isa MClassType
384 var sc
= mtype
.mclass
385 if not parents
.has
(sc
) or sc
== objectclass
then
386 # Skip the warning on generated code
387 if ntype
.location
.file
!= null and not ntype
.location
.file
.filename
.is_empty
then
388 warning
(ntype
, "Warning: superfluous super-class {mtype} in class {mclassdef.mclass}.")
390 else if not seen_parents
.has_key
(sc
) then
391 seen_parents
[sc
] = ntype
393 warning
(ntype
, "Warning: duplicated super-class {mtype} in class {mclassdef.mclass}.")
399 # Register the nclassdef associated to each mclassdef
400 # FIXME: why not refine the `MClassDef` class with a nullable attribute?
401 var mclassdef2nclassdef
: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
403 # Return the static type associated to the node `ntype`.
404 # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
405 # In case of problem, an error is displayed on `ntype` and null is returned.
406 # FIXME: the name "resolve_mtype" is awful
407 fun resolve_mtype_unchecked
(mmodule
: MModule, mclassdef
: nullable MClassDef, ntype
: AType, with_virtual
: Bool): nullable MType
409 var name
= ntype
.n_id
.text
413 if mclassdef
!= null and with_virtual
then
414 var prop
= try_get_mproperty_by_name
(ntype
, mclassdef
, name
).as(nullable MVirtualTypeProp)
416 if not ntype
.n_types
.is_empty
then
417 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
419 res
= prop
.mvirtualtype
420 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
426 # Check parameter type
427 if mclassdef
!= null and mclassdef
.parameter_names
.has
(name
) then
428 if not ntype
.n_types
.is_empty
then
429 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
431 for i
in [0..mclassdef
.parameter_names
.length
[ do
432 if mclassdef
.parameter_names
[i
] == name
then
433 res
= mclassdef
.mclass
.mclass_type
.arguments
[i
]
434 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
443 var mclass
= try_get_mclass_by_name
(ntype
, mmodule
, name
)
444 if mclass
!= null then
445 var arity
= ntype
.n_types
.length
446 if arity
!= mclass
.arity
then
448 error
(ntype
, "Type error: '{name}' is a generic class.")
449 else if mclass
.arity
== 0 then
450 error
(ntype
, "Type error: '{name}' is not a generic class.")
452 error
(ntype
, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
457 res
= mclass
.mclass_type
458 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
462 var mtypes
= new Array[MType]
463 for nt
in ntype
.n_types
do
464 var mt
= resolve_mtype_unchecked
(mmodule
, mclassdef
, nt
, with_virtual
)
465 if mt
== null then return null # Forward error
468 res
= mclass
.get_mtype
(mtypes
)
469 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
475 # If everything fail, then give up :(
476 error
(ntype
, "Type error: class {name} not found in module {mmodule}.")
480 # Return the static type associated to the node `ntype`.
481 # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
482 # In case of problem, an error is displayed on `ntype` and null is returned.
483 # FIXME: the name "resolve_mtype" is awful
484 fun resolve_mtype
(mmodule
: MModule, mclassdef
: nullable MClassDef, ntype
: AType): nullable MType
486 var mtype
= ntype
.mtype
487 if mtype
== null then mtype
= resolve_mtype_unchecked
(mmodule
, mclassdef
, ntype
, true)
488 if mtype
== null then return null # Forward error
490 if ntype
.checked_mtype
then return mtype
491 if mtype
isa MGenericType then
492 var mclass
= mtype
.mclass
493 for i
in [0..mclass
.arity
[ do
494 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
495 var nt
= ntype
.n_types
[i
]
496 var mt
= resolve_mtype
(mmodule
, mclassdef
, nt
)
497 if mt
== null then return null # forward error
499 if mclassdef
!= null then anchor
= mclassdef
.bound_mtype
else anchor
= null
500 if not mt
.is_subtype
(mmodule
, anchor
, bound
) then
501 error
(nt
, "Type error: expected {bound}, got {mt}")
506 ntype
.checked_mtype
= true
513 # Flag that indicate if the class building is already completed
514 var build_classes_is_done
: Bool = false
515 # What is the AClassdef associated to a `MClass`?
516 # Used to check multiple definition of a class.
517 var mclass2nclassdef
: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
520 redef class AClassdef
521 # The associated MClass once build by a `ModelBuilder`
522 var mclass
: nullable MClass
523 # The associated MClassDef once build by a `ModelBuilder`
524 var mclassdef
: nullable MClassDef
525 # All (self and other) definitions for the same mclassdef
526 var all_defs
: nullable Array[AClassdef]
529 redef class AClasskind
530 # The class kind associated with the AST node class
531 private fun mkind
: MClassKind is abstract
533 redef class AConcreteClasskind
534 redef fun mkind
do return concrete_kind
536 redef class AAbstractClasskind
537 redef fun mkind
do return abstract_kind
539 redef class AInterfaceClasskind
540 redef fun mkind
do return interface_kind
542 redef class AEnumClasskind
543 redef fun mkind
do return enum_kind
545 redef class AExternClasskind
546 redef fun mkind
do return extern_kind
549 redef class AFormaldef
550 # The associated parameter type
551 var mtype
: nullable MParameterType = null
553 # The associated bound
554 var bound
: nullable MType = null
558 # The mtype associated to the node
559 var mtype
: nullable MType = null
561 # Is the mtype a valid one?
562 var checked_mtype
: Bool = false