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 mclass
= new MClass(mmodule
, name
, arity
, mkind
, mvisibility
)
80 #print "new class {mclass}"
81 else if nclassdef
isa AStdClassdef and nmodule
.mclass2nclassdef
.has_key
(mclass
) then
82 error
(nclassdef
, "Error: A class {name} is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
84 else if nclassdef
isa AStdClassdef and nclassdef
.n_kwredef
== null then
85 error
(nclassdef
, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
87 else if mclass
.arity
!= arity
then
88 error
(nclassdef
, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
90 else if nkind
!= null and mkind
!= concrete_kind
and mclass
.kind
!= mkind
then
91 error
(nkind
, "Error: refinement changed the kind from a {mclass.kind} to a {mkind}")
92 else if nvisibility
!= null and mvisibility
!= public_visibility
and mclass
.visibility
!= mvisibility
then
93 error
(nvisibility
, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
95 nclassdef
.mclass
= mclass
96 nmodule
.mclass2nclassdef
[mclass
] = nclassdef
99 # Visit the AST and create the `MClassDef` objects
100 private fun build_a_mclassdef
(nmodule
: AModule, nclassdef
: AClassdef)
102 var mmodule
= nmodule
.mmodule
.as(not null)
103 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
104 var mclass
= nclassdef
.mclass
105 if mclass
== null then return # Skip error
106 #var mclassdef = nclassdef.mclassdef.as(not null)
108 var names
= new Array[String]
109 var bounds
= new Array[MType]
110 if nclassdef
isa AStdClassdef and mclass
.arity
> 0 then
111 # Collect formal parameter names
112 for i
in [0..mclass
.arity
[ do
113 var nfd
= nclassdef
.n_formaldefs
[i
]
114 var ptname
= nfd
.n_id
.text
115 if names
.has
(ptname
) then
116 error
(nfd
, "Error: A formal parameter type `{ptname}' already exists")
120 nfd
.mtype
= mclass
.mclass_type
.arguments
[i
].as(MParameterType)
123 # Revolve bound for formal parameter names
124 for i
in [0..mclass
.arity
[ do
125 var nfd
= nclassdef
.n_formaldefs
[i
]
126 var nfdt
= nfd
.n_type
128 var bound
= resolve_mtype_unchecked
(nclassdef
, nfdt
, false)
129 if bound
== null then return # Forward error
130 if bound
.need_anchor
then
132 error
(nfd
, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
137 else if mclass
.mclassdefs
.is_empty
then
138 # No bound, then implicitely bound by nullable Object
139 var bound
= objectclass
.mclass_type
.as_nullable
144 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
151 var bound_mtype
= mclass
.get_mtype
(bounds
)
152 var mclassdef
= new MClassDef(mmodule
, bound_mtype
, nclassdef
.location
, names
)
153 nclassdef
.mclassdef
= mclassdef
154 self.mclassdef2nclassdef
[mclassdef
] = nclassdef
156 if mclassdef
.is_intro
then
157 self.toolcontext
.info
("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
159 self.toolcontext
.info
("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
163 # Visit the AST and set the super-types of the `MClassDef` objects
164 private fun collect_a_mclassdef_inheritance
(nmodule
: AModule, nclassdef
: AClassdef)
166 var mmodule
= nmodule
.mmodule
.as(not null)
167 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
168 var mclass
= nclassdef
.mclass
.as(not null)
169 var mclassdef
= nclassdef
.mclassdef
.as(not null)
171 var specobject
= true
172 var supertypes
= new Array[MClassType]
173 if nclassdef
isa AStdClassdef then
174 for nsc
in nclassdef
.n_superclasses
do
176 var ntype
= nsc
.n_type
177 var mtype
= resolve_mtype_unchecked
(nclassdef
, ntype
, false)
178 if mtype
== null then continue # Skip because of error
179 if not mtype
isa MClassType then
180 error
(ntype
, "Error: supertypes cannot be a formal type")
184 #print "new super : {mclass} < {mtype}"
187 if specobject
and mclass
.name
!= "Object" and objectclass
!= null and mclassdef
.is_intro
then
188 supertypes
.add objectclass
.mclass_type
191 mclassdef
.set_supertypes
(supertypes
)
192 if not supertypes
.is_empty
then self.toolcontext
.info
("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
195 # Check the validity of the specialization heirarchy
196 private fun check_supertypes
(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 mclass
= nclassdef
.mclass
.as(not null)
201 var mclassdef
= nclassdef
.mclassdef
.as(not null)
203 for s
in mclassdef
.supertypes
do
204 if s
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, mclassdef
.bound_mtype
) then
205 error
(nclassdef
, "Error: Inheritance loop for class {mclass} with type {s}")
210 # Build the classes of the module `nmodule`.
211 # REQUIRE: classes of imported modules are already build. (let `phase` do the job)
212 private fun build_classes
(nmodule
: AModule)
214 var errcount
= toolcontext
.error_count
215 # Force building recursively
216 if nmodule
.build_classes_is_done
then return
217 nmodule
.build_classes_is_done
= true
218 var mmodule
= nmodule
.mmodule
.as(not null)
219 for imp
in mmodule
.in_importation
.direct_greaters
do
221 build_classes
(mmodule2nmodule
[imp
])
224 if errcount
!= toolcontext
.error_count
then return
227 for nclassdef
in nmodule
.n_classdefs
do
228 self.build_a_mclass
(nmodule
, nclassdef
)
231 if errcount
!= toolcontext
.error_count
then return
233 # Create all classdefs
234 for nclassdef
in nmodule
.n_classdefs
do
235 self.build_a_mclassdef
(nmodule
, nclassdef
)
238 if errcount
!= toolcontext
.error_count
then return
240 # Create inheritance on all classdefs
241 for nclassdef
in nmodule
.n_classdefs
do
242 self.collect_a_mclassdef_inheritance
(nmodule
, nclassdef
)
245 if errcount
!= toolcontext
.error_count
then return
247 # Create the mclassdef hierarchy
248 for nclassdef
in nmodule
.n_classdefs
do
249 var mclassdef
= nclassdef
.mclassdef
.as(not null)
250 mclassdef
.add_in_hierarchy
253 if errcount
!= toolcontext
.error_count
then return
256 for nclassdef
in nmodule
.n_classdefs
do
257 self.check_supertypes
(nmodule
, nclassdef
)
260 if errcount
!= toolcontext
.error_count
then return
262 # Check unchecked ntypes
263 for nclassdef
in nmodule
.n_classdefs
do
264 if nclassdef
isa AStdClassdef then
265 # check bound of formal parameter
266 for nfd
in nclassdef
.n_formaldefs
do
267 var nfdt
= nfd
.n_type
268 if nfdt
!= null and nfdt
.mtype
!= null then
269 var bound
= resolve_mtype
(nclassdef
, nfdt
)
270 if bound
== null then return # Forward error
273 # check declared super types
274 for nsc
in nclassdef
.n_superclasses
do
275 var ntype
= nsc
.n_type
276 if ntype
.mtype
!= null then
277 var mtype
= resolve_mtype
(nclassdef
, ntype
)
278 if mtype
== null then return # Forward error
284 if errcount
!= toolcontext
.error_count
then return
286 # Check clash of ancestors
287 for nclassdef
in nmodule
.n_classdefs
do
288 var mclassdef
= nclassdef
.mclassdef
.as(not null)
289 var superclasses
= new HashMap[MClass, MClassType]
290 for scd
in mclassdef
.in_hierarchy
.greaters
do
291 for st
in scd
.supertypes
do
292 if not superclasses
.has_key
(st
.mclass
) then
293 superclasses
[st
.mclass
] = st
294 else if superclasses
[st
.mclass
] != st
then
295 var st1
= superclasses
[st
.mclass
].resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
296 var st2
= st
.resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
298 error
(nclassdef
, "Error: Incompatibles ancestors for {mclassdef.mclass}: {st1}, {st2}")
305 if errcount
!= toolcontext
.error_count
then return
307 # TODO: Check that the super-class is not intrusive
309 # Check that the superclasses are not already known (by transitivity)
310 for nclassdef
in nmodule
.n_classdefs
do
311 if not nclassdef
isa AStdClassdef then continue
312 var mclassdef
= nclassdef
.mclassdef
.as(not null)
314 # Get the direct superclasses
315 # Since we are a mclassdef, just look at the mclassdef hierarchy
316 var parents
= new Array[MClass]
317 for sup
in mclassdef
.in_hierarchy
.direct_greaters
do
318 parents
.add
(sup
.mclass
)
321 # Used to track duplicates of superclasses
322 var seen_parents
= new ArrayMap[MClass, AType]
325 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
327 # Check each declared superclass to see if it belong to the direct superclass
328 for nsc
in nclassdef
.n_superclasses
do
329 var ntype
= nsc
.n_type
330 var mtype
= ntype
.mtype
331 if mtype
== null then continue
332 assert mtype
isa MClassType
333 var sc
= mtype
.mclass
334 if not parents
.has
(sc
) or sc
== objectclass
then
335 warning
(ntype
, "Warning: superfluous super-class {mtype} in class {mclassdef.mclass}.")
336 else if not seen_parents
.has_key
(sc
) then
337 seen_parents
[sc
] = ntype
339 warning
(ntype
, "Warning: duplicated super-class {mtype} in class {mclassdef.mclass}.")
345 # Register the nclassdef associated to each mclassdef
346 # FIXME: why not refine the `MClassDef` class with a nullable attribute?
347 var mclassdef2nclassdef
: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
349 # Return the static type associated to the node `ntype`.
350 # `nclassdef` is the context where the call is made (used to understand formal types)
351 # The mmodule used as context is `nclassdef.mmodule`
352 # In case of problem, an error is displayed on `ntype` and null is returned.
353 # FIXME: the name "resolve_mtype" is awful
354 fun resolve_mtype_unchecked
(nclassdef
: AClassdef, ntype
: AType, with_virtual
: Bool): nullable MType
356 var name
= ntype
.n_id
.text
357 var mclassdef
= nclassdef
.mclassdef
358 var mmodule
= nclassdef
.parent
.as(AModule).mmodule
.as(not null)
362 if mclassdef
!= null and with_virtual
then
363 var prop
= try_get_mproperty_by_name
(ntype
, mclassdef
, name
).as(nullable MVirtualTypeProp)
365 if not ntype
.n_types
.is_empty
then
366 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
368 res
= prop
.mvirtualtype
369 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
375 # Check parameter type
376 if mclassdef
!= null and mclassdef
.parameter_names
.has
(name
) then
377 if not ntype
.n_types
.is_empty
then
378 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
380 for i
in [0..mclassdef
.parameter_names
.length
[ do
381 if mclassdef
.parameter_names
[i
] == name
then
382 res
= mclassdef
.mclass
.mclass_type
.arguments
[i
]
383 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
392 var mclass
= try_get_mclass_by_name
(ntype
, mmodule
, name
)
393 if mclass
!= null then
394 var arity
= ntype
.n_types
.length
395 if arity
!= mclass
.arity
then
397 error
(ntype
, "Type error: '{name}' is a generic class.")
398 else if mclass
.arity
== 0 then
399 error
(ntype
, "Type error: '{name}' is not a generic class.")
401 error
(ntype
, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
406 res
= mclass
.mclass_type
407 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
411 var mtypes
= new Array[MType]
412 for nt
in ntype
.n_types
do
413 var mt
= resolve_mtype_unchecked
(nclassdef
, nt
, with_virtual
)
414 if mt
== null then return null # Forward error
417 res
= mclass
.get_mtype
(mtypes
)
418 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
424 # If everything fail, then give up :(
425 error
(ntype
, "Type error: class {name} not found in module {mmodule}.")
429 # Return the static type associated to the node `ntype`.
430 # `nclassdef` is the context where the call is made (used to understand formal types)
431 # The mmodule used as context is `nclassdef.mmodule`
432 # In case of problem, an error is displayed on `ntype` and null is returned.
433 # FIXME: the name "resolve_mtype" is awful
434 fun resolve_mtype
(nclassdef
: AClassdef, ntype
: AType): nullable MType
436 var mtype
= ntype
.mtype
437 if mtype
== null then mtype
= resolve_mtype_unchecked
(nclassdef
, ntype
, true)
438 if mtype
== null then return null # Forward error
440 if ntype
.checked_mtype
then return mtype
441 if mtype
isa MGenericType then
442 var mmodule
= nclassdef
.parent
.as(AModule).mmodule
.as(not null)
443 var mclassdef
= nclassdef
.mclassdef
444 var mclass
= mtype
.mclass
445 for i
in [0..mclass
.arity
[ do
446 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
447 var nt
= ntype
.n_types
[i
]
448 var mt
= resolve_mtype
(nclassdef
, nt
)
449 if mt
== null then return null # forward error
450 if not mt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, bound
) then
451 error
(nt
, "Type error: expected {bound}, got {mt}")
456 ntype
.checked_mtype
= true
463 # Flag that indicate if the class building is already completed
464 var build_classes_is_done
: Bool = false
465 # What is the AClassdef associated to a `MClass`?
466 # Used to check multiple definition of a class.
467 var mclass2nclassdef
: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
470 redef class AClassdef
471 # The associated MClass once build by a `ModelBuilder`
472 var mclass
: nullable MClass
473 # The associated MClassDef once build by a `ModelBuilder`
474 var mclassdef
: nullable MClassDef
477 redef class AClasskind
478 # The class kind associated with the AST node class
479 private fun mkind
: MClassKind is abstract
481 redef class AConcreteClasskind
482 redef fun mkind
do return concrete_kind
484 redef class AAbstractClasskind
485 redef fun mkind
do return abstract_kind
487 redef class AInterfaceClasskind
488 redef fun mkind
do return interface_kind
490 redef class AEnumClasskind
491 redef fun mkind
do return enum_kind
493 redef class AExternClasskind
494 redef fun mkind
do return extern_kind
497 redef class AFormaldef
498 # The associated parameter type
499 var mtype
: nullable MParameterType = null
501 # The associated bound
502 var bound
: nullable MType = null
506 # The mtype associated to the node
507 var mtype
: nullable MType = null
509 # Is the mtype a valid one?
510 var checked_mtype
: Bool = false