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.")
58 else if nclassdef
isa ATopClassdef then
61 mkind
= interface_kind
63 mvisibility
= public_visibility
64 else if nclassdef
isa AMainClassdef then
69 mvisibility
= public_visibility
74 var mclass
= try_get_mclass_by_name
(nclassdef
, mmodule
, name
)
75 if mclass
== null then
76 mclass
= new MClass(mmodule
, name
, arity
, mkind
, mvisibility
)
77 #print "new class {mclass}"
78 else if nclassdef
isa AStdClassdef and nmodule
.mclass2nclassdef
.has_key
(mclass
) then
79 error
(nclassdef
, "Error: A class {name} is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
81 else if nclassdef
isa AStdClassdef and nclassdef
.n_kwredef
== null then
82 error
(nclassdef
, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
84 else if mclass
.arity
!= arity
then
85 error
(nclassdef
, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
87 else if nkind
!= null and mkind
!= concrete_kind
and mclass
.kind
!= mkind
then
88 error
(nkind
, "Error: refinement changed the kind from a {mclass.kind} to a {mkind}")
89 else if nvisibility
!= null and mvisibility
!= public_visibility
and mclass
.visibility
!= mvisibility
then
90 error
(nvisibility
, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
92 nclassdef
.mclass
= mclass
93 nmodule
.mclass2nclassdef
[mclass
] = nclassdef
96 # Visit the AST and create the MClassDef objects
97 private fun build_a_mclassdef
(nmodule
: AModule, nclassdef
: AClassdef)
99 var mmodule
= nmodule
.mmodule
.as(not null)
100 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
101 var mclass
= nclassdef
.mclass
102 if mclass
== null then return # Skip error
103 #var mclassdef = nclassdef.mclassdef.as(not null)
105 var names
= new Array[String]
106 var bounds
= new Array[MType]
107 if nclassdef
isa AStdClassdef and mclass
.arity
> 0 then
108 # Collect formal parameter names
109 for i
in [0..mclass
.arity
[ do
110 var nfd
= nclassdef
.n_formaldefs
[i
]
111 var ptname
= nfd
.n_id
.text
112 if names
.has
(ptname
) then
113 error
(nfd
, "Error: A formal parameter type `{ptname}' already exists")
119 # Revolve bound for formal parameter names
120 for i
in [0..mclass
.arity
[ do
121 var nfd
= nclassdef
.n_formaldefs
[i
]
122 var nfdt
= nfd
.n_type
124 var bound
= resolve_mtype_unchecked
(nclassdef
, nfdt
, false)
125 if bound
== null then return # Forward error
126 if bound
.need_anchor
then
128 error
(nfd
, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
132 else if mclass
.mclassdefs
.is_empty
then
133 # No bound, then implicitely bound by nullable Object
134 bounds
.add
(objectclass
.mclass_type
.as_nullable
)
137 bounds
.add
(mclass
.intro
.bound_mtype
.arguments
[i
])
142 var bound_mtype
= mclass
.get_mtype
(bounds
)
143 var mclassdef
= new MClassDef(mmodule
, bound_mtype
, nclassdef
.location
, names
)
144 nclassdef
.mclassdef
= mclassdef
145 self.mclassdef2nclassdef
[mclassdef
] = nclassdef
147 if mclassdef
.is_intro
then
148 self.toolcontext
.info
("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
150 self.toolcontext
.info
("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
154 # Visit the AST and set the super-types of the MClassdef objects
155 private fun collect_a_mclassdef_inheritance
(nmodule
: AModule, nclassdef
: AClassdef)
157 var mmodule
= nmodule
.mmodule
.as(not null)
158 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
159 var mclass
= nclassdef
.mclass
.as(not null)
160 var mclassdef
= nclassdef
.mclassdef
.as(not null)
162 var specobject
= true
163 var supertypes
= new Array[MClassType]
164 if nclassdef
isa AStdClassdef then
165 for nsc
in nclassdef
.n_superclasses
do
167 var ntype
= nsc
.n_type
168 var mtype
= resolve_mtype_unchecked
(nclassdef
, ntype
, false)
169 if mtype
== null then continue # Skip because of error
170 if not mtype
isa MClassType then
171 error
(ntype
, "Error: supertypes cannot be a formal type")
175 #print "new super : {mclass} < {mtype}"
178 if specobject
and mclass
.name
!= "Object" and objectclass
!= null and mclassdef
.is_intro
then
179 supertypes
.add objectclass
.mclass_type
182 mclassdef
.set_supertypes
(supertypes
)
183 if not supertypes
.is_empty
then self.toolcontext
.info
("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
186 # Check the validity of the specialization heirarchy
187 private fun check_supertypes
(nmodule
: AModule, nclassdef
: AClassdef)
189 var mmodule
= nmodule
.mmodule
.as(not null)
190 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
191 var mclass
= nclassdef
.mclass
.as(not null)
192 var mclassdef
= nclassdef
.mclassdef
.as(not null)
194 for s
in mclassdef
.supertypes
do
195 if s
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, mclassdef
.bound_mtype
) then
196 error
(nclassdef
, "Error: Inheritance loop for class {mclass} with type {s}")
201 # Build the classes of the module `nmodule'.
202 # REQUIRE: classes of imported modules are already build. (let `phase' do the job)
203 private fun build_classes
(nmodule
: AModule)
205 var errcount
= toolcontext
.error_count
206 # Force building recursively
207 if nmodule
.build_classes_is_done
then return
208 nmodule
.build_classes_is_done
= true
209 var mmodule
= nmodule
.mmodule
.as(not null)
210 for imp
in mmodule
.in_importation
.direct_greaters
do
212 build_classes
(mmodule2nmodule
[imp
])
215 if errcount
!= toolcontext
.error_count
then return
218 for nclassdef
in nmodule
.n_classdefs
do
219 self.build_a_mclass
(nmodule
, nclassdef
)
222 if errcount
!= toolcontext
.error_count
then return
224 # Create all classdefs
225 for nclassdef
in nmodule
.n_classdefs
do
226 self.build_a_mclassdef
(nmodule
, nclassdef
)
229 if errcount
!= toolcontext
.error_count
then return
231 # Create inheritance on all classdefs
232 for nclassdef
in nmodule
.n_classdefs
do
233 self.collect_a_mclassdef_inheritance
(nmodule
, nclassdef
)
236 if errcount
!= toolcontext
.error_count
then return
238 # Create the mclassdef hierarchy
239 for nclassdef
in nmodule
.n_classdefs
do
240 var mclassdef
= nclassdef
.mclassdef
.as(not null)
241 mclassdef
.add_in_hierarchy
244 if errcount
!= toolcontext
.error_count
then return
247 for nclassdef
in nmodule
.n_classdefs
do
248 self.check_supertypes
(nmodule
, nclassdef
)
251 if errcount
!= toolcontext
.error_count
then return
253 # Check unchecked ntypes
254 for nclassdef
in nmodule
.n_classdefs
do
255 if nclassdef
isa AStdClassdef then
256 # check bound of formal parameter
257 for nfd
in nclassdef
.n_formaldefs
do
258 var nfdt
= nfd
.n_type
259 if nfdt
!= null and nfdt
.mtype
!= null then
260 var bound
= resolve_mtype
(nclassdef
, nfdt
)
261 if bound
== null then return # Forward error
264 # check declared super types
265 for nsc
in nclassdef
.n_superclasses
do
266 var ntype
= nsc
.n_type
267 if ntype
.mtype
!= null then
268 var mtype
= resolve_mtype
(nclassdef
, ntype
)
269 if mtype
== null then return # Forward error
275 if errcount
!= toolcontext
.error_count
then return
277 # Check clash of ancestors
278 for nclassdef
in nmodule
.n_classdefs
do
279 var mclassdef
= nclassdef
.mclassdef
.as(not null)
280 var superclasses
= new HashMap[MClass, MClassType]
281 for scd
in mclassdef
.in_hierarchy
.greaters
do
282 for st
in scd
.supertypes
do
283 if not superclasses
.has_key
(st
.mclass
) then
284 superclasses
[st
.mclass
] = st
285 else if superclasses
[st
.mclass
] != st
then
286 var st1
= superclasses
[st
.mclass
].resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
287 var st2
= st
.resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
289 error
(nclassdef
, "Error: Incompatibles ancestors for {mclassdef.mclass}: {st1}, {st2}")
296 if errcount
!= toolcontext
.error_count
then return
298 # TODO: Check that the super-class is not intrusive
300 # Check that the superclasses are not already known (by transitivity)
301 for nclassdef
in nmodule
.n_classdefs
do
302 if not nclassdef
isa AStdClassdef then continue
303 var mclassdef
= nclassdef
.mclassdef
.as(not null)
305 # Get the direct superclasses
306 # Since we are a mclassdef, just look at the mclassdef hierarchy
307 var parents
= new Array[MClass]
308 for sup
in mclassdef
.in_hierarchy
.direct_greaters
do
309 parents
.add
(sup
.mclass
)
312 # Used to track duplicates of superclasses
313 var seen_parents
= new ArrayMap[MClass, AType]
316 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
318 # Check each declared superclass to see if it belong to the direct superclass
319 for nsc
in nclassdef
.n_superclasses
do
320 var ntype
= nsc
.n_type
321 var mtype
= ntype
.mtype
322 if mtype
== null then continue
323 assert mtype
isa MClassType
324 var sc
= mtype
.mclass
325 if not parents
.has
(sc
) or sc
== objectclass
then
326 warning
(ntype
, "Warning: superfluous super-class {mtype} in class {mclassdef.mclass}.")
327 else if not seen_parents
.has_key
(sc
) then
328 seen_parents
[sc
] = ntype
330 warning
(ntype
, "Warning: duplicated super-class {mtype} in class {mclassdef.mclass}.")
336 # Register the nclassdef associated to each mclassdef
337 # FIXME: why not refine the MClassDef class with a nullable attribute?
338 var mclassdef2nclassdef
: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
340 # Return the static type associated to the node `ntype'.
341 # `classdef' is the context where the call is made (used to understand formal types)
342 # The mmodule used as context is `nclassdef.mmodule'
343 # In case of problem, an error is displayed on `ntype' and null is returned.
344 # FIXME: the name "resolve_mtype" is awful
345 fun resolve_mtype_unchecked
(nclassdef
: AClassdef, ntype
: AType, with_virtual
: Bool): nullable MType
347 var name
= ntype
.n_id
.text
348 var mclassdef
= nclassdef
.mclassdef
349 var mmodule
= nclassdef
.parent
.as(AModule).mmodule
.as(not null)
353 if mclassdef
!= null and with_virtual
then
354 var prop
= try_get_mproperty_by_name
(ntype
, mclassdef
, name
).as(nullable MVirtualTypeProp)
356 if not ntype
.n_types
.is_empty
then
357 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
359 res
= prop
.mvirtualtype
360 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
366 # Check parameter type
367 if mclassdef
!= null and mclassdef
.parameter_names
.has
(name
) then
368 if not ntype
.n_types
.is_empty
then
369 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
371 for i
in [0..mclassdef
.parameter_names
.length
[ do
372 if mclassdef
.parameter_names
[i
] == name
then
373 res
= mclassdef
.mclass
.mclass_type
.arguments
[i
]
374 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
383 var mclass
= try_get_mclass_by_name
(ntype
, mmodule
, name
)
384 if mclass
!= null then
385 var arity
= ntype
.n_types
.length
386 if arity
!= mclass
.arity
then
388 error
(ntype
, "Type error: '{name}' is a generic class.")
389 else if mclass
.arity
== 0 then
390 error
(ntype
, "Type error: '{name}' is not a generic class.")
392 error
(ntype
, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
397 res
= mclass
.mclass_type
398 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
402 var mtypes
= new Array[MType]
403 for nt
in ntype
.n_types
do
404 var mt
= resolve_mtype_unchecked
(nclassdef
, nt
, with_virtual
)
405 if mt
== null then return null # Forward error
408 res
= mclass
.get_mtype
(mtypes
)
409 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
415 # If everything fail, then give up :(
416 error
(ntype
, "Type error: class {name} not found in module {mmodule}.")
420 # Return the static type associated to the node `ntype'.
421 # `classdef' is the context where the call is made (used to understand formal types)
422 # The mmodule used as context is `nclassdef.mmodule'
423 # In case of problem, an error is displayed on `ntype' and null is returned.
424 # FIXME: the name "resolve_mtype" is awful
425 fun resolve_mtype
(nclassdef
: AClassdef, ntype
: AType): nullable MType
427 var mtype
= ntype
.mtype
428 if mtype
== null then mtype
= resolve_mtype_unchecked
(nclassdef
, ntype
, true)
429 if mtype
== null then return null # Forward error
431 if ntype
.checked_mtype
then return mtype
432 if mtype
isa MGenericType then
433 var mmodule
= nclassdef
.parent
.as(AModule).mmodule
.as(not null)
434 var mclassdef
= nclassdef
.mclassdef
435 var mclass
= mtype
.mclass
436 for i
in [0..mclass
.arity
[ do
437 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
438 var nt
= ntype
.n_types
[i
]
439 var mt
= resolve_mtype
(nclassdef
, nt
)
440 if mt
== null then return null # forward error
441 if not mt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, bound
) then
442 error
(nt
, "Type error: expected {bound}, got {mt}")
447 ntype
.checked_mtype
= true
454 # Flag that indicate if the class building is already completed
455 var build_classes_is_done
: Bool = false
456 # What is the AClassdef associated to a MClass?
457 # Used to check multiple definition of a class.
458 var mclass2nclassdef
: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
461 redef class AClassdef
462 # The associated MClass once build by a `ModelBuilder'
463 var mclass
: nullable MClass
464 # The associated MClassDef once build by a `ModelBuilder'
465 var mclassdef
: nullable MClassDef
468 redef class AClasskind
469 # The class kind associated with the AST node class
470 private fun mkind
: MClassKind is abstract
472 redef class AConcreteClasskind
473 redef fun mkind
do return concrete_kind
475 redef class AAbstractClasskind
476 redef fun mkind
do return abstract_kind
478 redef class AInterfaceClasskind
479 redef fun mkind
do return interface_kind
481 redef class AEnumClasskind
482 redef fun mkind
do return enum_kind
484 redef class AExternClasskind
485 redef fun mkind
do return extern_kind
489 # The mtype associated to the node
490 var mtype
: nullable MType = null
492 # Is the mtype a valid one?
493 var checked_mtype
: Bool = false