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 nmodule
.mclass2nclassdef
[mclass
] = nclassdef
103 # Visit the AST and create the `MClassDef` objects
104 private fun build_a_mclassdef
(nmodule
: AModule, nclassdef
: AClassdef)
106 var mmodule
= nmodule
.mmodule
.as(not null)
107 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
108 var mclass
= nclassdef
.mclass
109 if mclass
== null then return # Skip error
110 #var mclassdef = nclassdef.mclassdef.as(not null)
112 var names
= new Array[String]
113 var bounds
= new Array[MType]
114 if nclassdef
isa AStdClassdef and mclass
.arity
> 0 then
115 # Collect formal parameter names
116 for i
in [0..mclass
.arity
[ do
117 var nfd
= nclassdef
.n_formaldefs
[i
]
118 var ptname
= nfd
.n_id
.text
119 if names
.has
(ptname
) then
120 error
(nfd
, "Error: A formal parameter type `{ptname}' already exists")
123 for c
in ptname
.chars
do if c
>= 'a' and c
<= 'z' then
124 warning
(nfd
, "Warning: lowercase in the formal parameter type {ptname}")
128 nfd
.mtype
= mclass
.mclass_type
.arguments
[i
].as(MParameterType)
131 # Revolve bound for formal parameter names
132 for i
in [0..mclass
.arity
[ do
133 var nfd
= nclassdef
.n_formaldefs
[i
]
134 var nfdt
= nfd
.n_type
136 var bound
= resolve_mtype_unchecked
(nclassdef
, nfdt
, false)
137 if bound
== null then return # Forward error
138 if bound
.need_anchor
then
140 error
(nfd
, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
145 else if mclass
.mclassdefs
.is_empty
then
146 # No bound, then implicitely bound by nullable Object
147 var bound
= objectclass
.mclass_type
.as_nullable
152 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
159 var bound_mtype
= mclass
.get_mtype
(bounds
)
160 var mclassdef
= new MClassDef(mmodule
, bound_mtype
, nclassdef
.location
, names
)
161 nclassdef
.mclassdef
= mclassdef
162 self.mclassdef2nclassdef
[mclassdef
] = nclassdef
164 if nclassdef
isa AStdClassdef then
165 var ndoc
= nclassdef
.n_doc
166 if ndoc
!= null then mclassdef
.mdoc
= ndoc
.to_mdoc
169 if mclassdef
.is_intro
then
170 self.toolcontext
.info
("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
172 self.toolcontext
.info
("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
176 # Visit the AST and set the super-types of the `MClassDef` objects
177 private fun collect_a_mclassdef_inheritance
(nmodule
: AModule, nclassdef
: AClassdef)
179 var mmodule
= nmodule
.mmodule
.as(not null)
180 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
181 var pointerclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Pointer")
182 var mclass
= nclassdef
.mclass
.as(not null)
183 var mclassdef
= nclassdef
.mclassdef
.as(not null)
185 var specobject
= true
186 var supertypes
= new Array[MClassType]
187 if nclassdef
isa AStdClassdef then
188 for nsc
in nclassdef
.n_superclasses
do
190 var ntype
= nsc
.n_type
191 var mtype
= resolve_mtype_unchecked
(nclassdef
, ntype
, false)
192 if mtype
== null then continue # Skip because of error
193 if not mtype
isa MClassType then
194 error
(ntype
, "Error: supertypes cannot be a formal type")
198 #print "new super : {mclass} < {mtype}"
201 if specobject
and mclassdef
.is_intro
then
202 if mclass
.kind
== extern_kind
then
203 if mclass
.name
== "Pointer" then
204 supertypes
.add objectclass
.mclass_type
206 supertypes
.add pointerclass
.mclass_type
208 else if mclass
.name
!= "Object" and objectclass
!= null then
209 supertypes
.add objectclass
.mclass_type
213 mclassdef
.set_supertypes
(supertypes
)
214 if not supertypes
.is_empty
then self.toolcontext
.info
("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
217 # Check the validity of the specialization heirarchy
218 private fun check_supertypes
(nmodule
: AModule, nclassdef
: AClassdef)
220 var mmodule
= nmodule
.mmodule
.as(not null)
221 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
222 var mclass
= nclassdef
.mclass
.as(not null)
223 var mclassdef
= nclassdef
.mclassdef
.as(not null)
225 for s
in mclassdef
.supertypes
do
226 if s
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, mclassdef
.bound_mtype
) then
227 error
(nclassdef
, "Error: Inheritance loop for class {mclass} with type {s}")
232 # Build the classes of the module `nmodule`.
233 # REQUIRE: classes of imported modules are already build. (let `phase` do the job)
234 private fun build_classes
(nmodule
: AModule)
236 var errcount
= toolcontext
.error_count
237 # Force building recursively
238 if nmodule
.build_classes_is_done
then return
239 nmodule
.build_classes_is_done
= true
240 var mmodule
= nmodule
.mmodule
.as(not null)
241 for imp
in mmodule
.in_importation
.direct_greaters
do
243 build_classes
(mmodule2nmodule
[imp
])
246 if errcount
!= toolcontext
.error_count
then return
249 for nclassdef
in nmodule
.n_classdefs
do
250 self.build_a_mclass
(nmodule
, nclassdef
)
253 if errcount
!= toolcontext
.error_count
then return
255 # Create all classdefs
256 for nclassdef
in nmodule
.n_classdefs
do
257 self.build_a_mclassdef
(nmodule
, nclassdef
)
260 if errcount
!= toolcontext
.error_count
then return
262 # Create inheritance on all classdefs
263 for nclassdef
in nmodule
.n_classdefs
do
264 self.collect_a_mclassdef_inheritance
(nmodule
, nclassdef
)
267 if errcount
!= toolcontext
.error_count
then return
269 # Create the mclassdef hierarchy
270 for nclassdef
in nmodule
.n_classdefs
do
271 var mclassdef
= nclassdef
.mclassdef
.as(not null)
272 mclassdef
.add_in_hierarchy
275 if errcount
!= toolcontext
.error_count
then return
278 for nclassdef
in nmodule
.n_classdefs
do
279 self.check_supertypes
(nmodule
, nclassdef
)
282 if errcount
!= toolcontext
.error_count
then return
284 # Check unchecked ntypes
285 for nclassdef
in nmodule
.n_classdefs
do
286 if nclassdef
isa AStdClassdef then
287 # check bound of formal parameter
288 for nfd
in nclassdef
.n_formaldefs
do
289 var nfdt
= nfd
.n_type
290 if nfdt
!= null and nfdt
.mtype
!= null then
291 var bound
= resolve_mtype
(nclassdef
, nfdt
)
292 if bound
== null then return # Forward error
295 # check declared super types
296 for nsc
in nclassdef
.n_superclasses
do
297 var ntype
= nsc
.n_type
298 if ntype
.mtype
!= null then
299 var mtype
= resolve_mtype
(nclassdef
, ntype
)
300 if mtype
== null then return # Forward error
306 if errcount
!= toolcontext
.error_count
then return
308 # Check clash of ancestors
309 for nclassdef
in nmodule
.n_classdefs
do
310 var mclassdef
= nclassdef
.mclassdef
.as(not null)
311 var superclasses
= new HashMap[MClass, MClassType]
312 for scd
in mclassdef
.in_hierarchy
.greaters
do
313 for st
in scd
.supertypes
do
314 if not superclasses
.has_key
(st
.mclass
) then
315 superclasses
[st
.mclass
] = st
316 else if superclasses
[st
.mclass
] != st
then
317 var st1
= superclasses
[st
.mclass
].resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
318 var st2
= st
.resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
320 error
(nclassdef
, "Error: Incompatibles ancestors for {mclassdef.mclass}: {st1}, {st2}")
327 if errcount
!= toolcontext
.error_count
then return
329 # TODO: Check that the super-class is not intrusive
331 # Check that the superclasses are not already known (by transitivity)
332 for nclassdef
in nmodule
.n_classdefs
do
333 if not nclassdef
isa AStdClassdef then continue
334 var mclassdef
= nclassdef
.mclassdef
.as(not null)
336 # Get the direct superclasses
337 # Since we are a mclassdef, just look at the mclassdef hierarchy
338 var parents
= new Array[MClass]
339 for sup
in mclassdef
.in_hierarchy
.direct_greaters
do
340 parents
.add
(sup
.mclass
)
343 # Used to track duplicates of superclasses
344 var seen_parents
= new ArrayMap[MClass, AType]
347 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
349 # Check each declared superclass to see if it belong to the direct superclass
350 for nsc
in nclassdef
.n_superclasses
do
351 var ntype
= nsc
.n_type
352 var mtype
= ntype
.mtype
353 if mtype
== null then continue
354 assert mtype
isa MClassType
355 var sc
= mtype
.mclass
356 if not parents
.has
(sc
) or sc
== objectclass
then
357 warning
(ntype
, "Warning: superfluous super-class {mtype} in class {mclassdef.mclass}.")
358 else if not seen_parents
.has_key
(sc
) then
359 seen_parents
[sc
] = ntype
361 warning
(ntype
, "Warning: duplicated super-class {mtype} in class {mclassdef.mclass}.")
367 # Register the nclassdef associated to each mclassdef
368 # FIXME: why not refine the `MClassDef` class with a nullable attribute?
369 var mclassdef2nclassdef
: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
371 # Return the static type associated to the node `ntype`.
372 # `nclassdef` is the context where the call is made (used to understand formal types)
373 # The mmodule used as context is `nclassdef.mmodule`
374 # In case of problem, an error is displayed on `ntype` and null is returned.
375 # FIXME: the name "resolve_mtype" is awful
376 fun resolve_mtype_unchecked
(nclassdef
: AClassdef, ntype
: AType, with_virtual
: Bool): nullable MType
378 var name
= ntype
.n_id
.text
379 var mclassdef
= nclassdef
.mclassdef
380 var mmodule
= nclassdef
.parent
.as(AModule).mmodule
.as(not null)
384 if mclassdef
!= null and with_virtual
then
385 var prop
= try_get_mproperty_by_name
(ntype
, mclassdef
, name
).as(nullable MVirtualTypeProp)
387 if not ntype
.n_types
.is_empty
then
388 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
390 res
= prop
.mvirtualtype
391 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
397 # Check parameter type
398 if mclassdef
!= null and mclassdef
.parameter_names
.has
(name
) then
399 if not ntype
.n_types
.is_empty
then
400 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
402 for i
in [0..mclassdef
.parameter_names
.length
[ do
403 if mclassdef
.parameter_names
[i
] == name
then
404 res
= mclassdef
.mclass
.mclass_type
.arguments
[i
]
405 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
414 var mclass
= try_get_mclass_by_name
(ntype
, mmodule
, name
)
415 if mclass
!= null then
416 var arity
= ntype
.n_types
.length
417 if arity
!= mclass
.arity
then
419 error
(ntype
, "Type error: '{name}' is a generic class.")
420 else if mclass
.arity
== 0 then
421 error
(ntype
, "Type error: '{name}' is not a generic class.")
423 error
(ntype
, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
428 res
= mclass
.mclass_type
429 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
433 var mtypes
= new Array[MType]
434 for nt
in ntype
.n_types
do
435 var mt
= resolve_mtype_unchecked
(nclassdef
, nt
, with_virtual
)
436 if mt
== null then return null # Forward error
439 res
= mclass
.get_mtype
(mtypes
)
440 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
446 # If everything fail, then give up :(
447 error
(ntype
, "Type error: class {name} not found in module {mmodule}.")
451 # Return the static type associated to the node `ntype`.
452 # `nclassdef` is the context where the call is made (used to understand formal types)
453 # The mmodule used as context is `nclassdef.mmodule`
454 # In case of problem, an error is displayed on `ntype` and null is returned.
455 # FIXME: the name "resolve_mtype" is awful
456 fun resolve_mtype
(nclassdef
: AClassdef, ntype
: AType): nullable MType
458 var mtype
= ntype
.mtype
459 if mtype
== null then mtype
= resolve_mtype_unchecked
(nclassdef
, ntype
, true)
460 if mtype
== null then return null # Forward error
462 if ntype
.checked_mtype
then return mtype
463 if mtype
isa MGenericType then
464 var mmodule
= nclassdef
.parent
.as(AModule).mmodule
.as(not null)
465 var mclassdef
= nclassdef
.mclassdef
466 var mclass
= mtype
.mclass
467 for i
in [0..mclass
.arity
[ do
468 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
469 var nt
= ntype
.n_types
[i
]
470 var mt
= resolve_mtype
(nclassdef
, nt
)
471 if mt
== null then return null # forward error
472 if not mt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, bound
) then
473 error
(nt
, "Type error: expected {bound}, got {mt}")
478 ntype
.checked_mtype
= true
485 # Flag that indicate if the class building is already completed
486 var build_classes_is_done
: Bool = false
487 # What is the AClassdef associated to a `MClass`?
488 # Used to check multiple definition of a class.
489 var mclass2nclassdef
: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
492 redef class AClassdef
493 # The associated MClass once build by a `ModelBuilder`
494 var mclass
: nullable MClass
495 # The associated MClassDef once build by a `ModelBuilder`
496 var mclassdef
: nullable MClassDef
499 redef class AClasskind
500 # The class kind associated with the AST node class
501 private fun mkind
: MClassKind is abstract
503 redef class AConcreteClasskind
504 redef fun mkind
do return concrete_kind
506 redef class AAbstractClasskind
507 redef fun mkind
do return abstract_kind
509 redef class AInterfaceClasskind
510 redef fun mkind
do return interface_kind
512 redef class AEnumClasskind
513 redef fun mkind
do return enum_kind
515 redef class AExternClasskind
516 redef fun mkind
do return extern_kind
519 redef class AFormaldef
520 # The associated parameter type
521 var mtype
: nullable MParameterType = null
523 # The associated bound
524 var bound
: nullable MType = null
528 # The mtype associated to the node
529 var mtype
: nullable MType = null
531 # Is the mtype a valid one?
532 var checked_mtype
: Bool = false