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]
50 if nclassdef
isa AStdClassdef then
51 var qid
= nclassdef
.n_qid
54 nkind
= nclassdef
.n_classkind
56 nvisibility
= nclassdef
.n_visibility
57 mvisibility
= nvisibility
.mvisibility
58 arity
= nclassdef
.n_formaldefs
.length
59 if mvisibility
== protected_visibility
then
60 error
(nvisibility
, "Error: only properties can be protected.")
62 else if mvisibility
== intrude_visibility
then
63 error
(nvisibility
, "Error: intrude is not a legal visibility for classes.")
66 # Collect formal parameter names
67 for i
in [0..arity
[ do
68 var nfd
= nclassdef
.n_formaldefs
[i
]
69 var ptname
= nfd
.n_id
.text
70 if names
.has
(ptname
) then
71 error
(nfd
, "Error: a formal parameter type `{ptname}` already exists.")
74 for c
in ptname
.chars
do if c
>= 'a' and c
<= 'z' then
75 warning
(nfd
, "formal-type-name", "Warning: lowercase in the formal parameter type `{ptname}`.")
80 mclass
= try_get_mclass_by_qid
(qid
, mmodule
)
81 if mclass
== null and (qid
.n_qualified
!= null or nclassdef
.n_kwredef
!= null) then
82 class_not_found
(qid
, mmodule
)
83 nclassdef
.is_broken
= true
87 else if nclassdef
isa ATopClassdef and nclassdef
.n_propdefs
.first
.as(AMethPropdef).n_methid
.collect_text
== "sys" then
88 # Special case to keep `sys` in object.
89 # Needed to keep working bootstrap and a working java FFI together.
90 # TODO: remove once safe to remove
93 mkind
= interface_kind
95 mvisibility
= public_visibility
96 mclass
= try_get_mclass_by_name
(nclassdef
, mmodule
, name
)
100 mkind
= concrete_kind
102 mvisibility
= public_visibility
103 mclass
= try_get_mclass_by_name
(nclassdef
, mmodule
, name
)
106 if mclass
== null then
107 # Check for conflicting class full-names in the package
108 if mmodule
.mgroup
!= null and mvisibility
>= protected_visibility
then
109 var mclasses
= model
.get_mclasses_by_name
(name
)
110 if mclasses
!= null then for other
in mclasses
do
111 if other
.intro_mmodule
.mgroup
!= null and other
.intro_mmodule
.mgroup
.mpackage
== mmodule
.mgroup
.mpackage
then
112 # Skip classes that are buggy
113 if other
.try_intro
== null then continue
114 warning
(nclassdef
, "full-name-conflict", "Error: a class named `{other.full_name}` is already defined in module `{other.intro_mmodule}` at {other.intro.location}.")
120 mclass
= new MClass(mmodule
, name
, nclassdef
.location
, names
, mkind
, mvisibility
)
121 #print "new class {mclass}"
122 else if nclassdef
isa AStdClassdef and nmodule
.mclass2nclassdef
.has_key
(mclass
) then
123 error
(nclassdef
, "Error: a class `{name}` is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
124 mclass
.is_broken
= true
126 else if nclassdef
isa AStdClassdef and nclassdef
.n_kwredef
== null then
127 error
(nclassdef
, "Redef Error: `{name}` is an imported class. Add the `redef` keyword to refine it.")
128 mclass
.is_broken
= true
130 else if arity
!= 0 and mclass
.arity
!= arity
then
131 error
(nclassdef
, "Redef Error: expected {mclass.arity} formal parameter(s) for {mclass.signature_to_s}; got {arity}.")
132 mclass
.is_broken
= true
134 else if nkind
!= null and mkind
!= concrete_kind
and mclass
.kind
!= mkind
then
135 error
(nkind
, "Redef Error: refinement changed the kind from `{mclass.kind}` to `{mkind}`.")
136 else if nvisibility
!= null and mvisibility
!= public_visibility
and mclass
.visibility
!= mvisibility
then
137 error
(nvisibility
, "Redef Error: refinement changed the visibility from `{mclass.visibility}` to `{mvisibility}`")
139 nclassdef
.mclass
= mclass
140 if not nmodule
.mclass2nclassdef
.has_key
(mclass
) then
141 nmodule
.mclass2nclassdef
[mclass
] = nclassdef
142 nclassdef
.all_defs
= [nclassdef
]
144 nmodule
.mclass2nclassdef
[mclass
].all_defs
.add
(nclassdef
)
148 # Visit the AST and create the `MClassDef` objects
149 private fun build_a_mclassdef
(nmodule
: AModule, nclassdef
: AClassdef)
151 var mmodule
= nmodule
.mmodule
.as(not null)
152 var mclass
= nclassdef
.mclass
153 if mclass
== null then return # Skip error
155 # In case of non-standard AClassdef, try to attach to an already existing mclassdef
156 var other_nclassdef
= nmodule
.mclass2nclassdef
[mclass
]
157 if other_nclassdef
!= nclassdef
then
158 assert not nclassdef
isa AStdClassdef
159 nclassdef
.mclassdef
= other_nclassdef
.mclassdef
163 var bound_mtype
= build_a_bound_mtype
(nmodule
, nclassdef
)
164 if bound_mtype
== null then return
165 var mclassdef
= new MClassDef(mmodule
, bound_mtype
, nclassdef
.location
)
166 nclassdef
.mclassdef
= mclassdef
167 self.mclassdef2nclassdef
[mclassdef
] = nclassdef
169 if nclassdef
isa AStdClassdef then
170 var ndoc
= nclassdef
.n_doc
172 var mdoc
= ndoc
.to_mdoc
173 mclassdef
.mdoc
= mdoc
174 mdoc
.original_mentity
= mclassdef
175 else if mclassdef
.is_intro
and mclass
.visibility
>= public_visibility
then
176 advice
(nclassdef
, "missing-doc", "Documentation warning: Undocumented public class `{mclass}`")
180 if mclassdef
.is_intro
then
181 self.toolcontext
.info
("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
183 self.toolcontext
.info
("{mclassdef} refines {mclass.kind} {mclass.full_name}", 3)
187 # Determine the type parameter bounds for `nclassdef`.
189 # In case of error, return `null`.
191 # REQUIRE: `nmodule.mmodule != null`
192 # REQUIRE: `nclassdef.mclass != null`
193 private fun build_a_bound_mtype
(nmodule
: AModule, nclassdef
: AClassdef): nullable MClassType
195 var mmodule
= nmodule
.mmodule
.as(not null)
196 var mclass
= nclassdef
.mclass
.as(not null)
198 var bounds
= new Array[MType]
199 if nclassdef
isa AStdClassdef and mclass
.arity
> 0 then
200 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
202 # Revolve bound for formal parameters
203 for i
in [0..mclass
.arity
[ do
204 if nclassdef
.n_formaldefs
.is_empty
then
206 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
211 var nfd
= nclassdef
.n_formaldefs
[i
]
212 var pname
= mclass
.mparameters
[i
].name
213 if nfd
.n_id
.text
!= pname
then
214 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}`.")
216 var nfdt
= nfd
.n_type
218 var bound
= resolve_mtype3_unchecked
(mmodule
, null, null, nfdt
, false)
219 if bound
== null then return null # Forward error
220 if bound
.need_anchor
then
222 error
(nfd
, "Error: formal parameter type `{pname}` bounded with a formal parameter type.")
227 else if mclass
.mclassdefs
.is_empty
then
228 if objectclass
== null then
229 error
(nfd
, "Error: formal parameter type `{pname}` unbounded but no `Object` class exists.")
232 # No bound, then implicitely bound by nullable Object
233 var bound
= objectclass
.mclass_type
.as_nullable
238 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
245 return mclass
.get_mtype
(bounds
)
248 # Visit the AST and set the super-types of the `MClassDef` objects
249 private fun build_a_mclassdef_inheritance
(nmodule
: AModule, nclassdef
: AClassdef)
251 var mmodule
= nmodule
.mmodule
252 if mmodule
== null then return
253 var mclass
= nclassdef
.mclass
254 if mclass
== null then return
255 var mclassdef
= nclassdef
.mclassdef
256 if mclassdef
== null then return
258 var supertypes
= collect_supertypes
(nmodule
, nclassdef
, mclassdef
.is_intro
)
259 mclassdef
.set_supertypes
(supertypes
)
260 if not supertypes
.is_empty
then self.toolcontext
.info
("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
263 # List the supertypes specified or implied by `nclassdef`.
265 # REQUIRE: `nmodule.mmodule != null`
266 # REQUIRE: `nclassdef.mclass != null`
267 private fun collect_supertypes
(nmodule
: AModule, nclassdef
: AClassdef,
268 is_intro
: Bool): Array[MClassType]
270 var mmodule
= nmodule
.mmodule
.as(not null)
271 var mclass
= nclassdef
.mclass
.as(not null)
272 var name
= mclass
.name
273 var kind
= mclass
.kind
275 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
276 var pointerclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Pointer")
278 # Do we need to specify Object as a super class?
279 var specobject
= true
281 # Do we need to specify Pointer as a super class? (is only valid
282 # if `nclassdef` is an extern class)
283 var specpointer
= true
285 var supertypes
= new Array[MClassType]
286 if nclassdef
isa AStdClassdef then
287 for nsc
in nclassdef
.n_superclasses
do
289 var ntype
= nsc
.n_type
290 var mtype
= resolve_mtype3_unchecked
(mmodule
, mclass
, null,
292 if mtype
== null then continue # Skip because of error
293 if not mtype
isa MClassType then
294 error
(ntype
, "Error: a supertype cannot be a formal type.")
297 var superclass
= mtype
.mclass
298 var super_kind
= superclass
.kind
299 if not kind
.can_specialize
(super_kind
) then
300 error
(ntype
, "Error: {kind} `{mclass}` cannot specialize {super_kind} `{superclass}`.")
303 #print "new super : {mclass} < {mtype}"
304 if super_kind
== extern_kind
then specpointer
= false
308 if is_intro
and objectclass
!= null then
309 if kind
== extern_kind
and name
!= "Pointer" then
310 # it is an extern class, but not a Pointer
311 if pointerclass
== null then
312 error
(nclassdef
, "Error: `Pointer` must be defined first.")
315 if specpointer
then supertypes
.add pointerclass
.mclass_type
316 else if specobject
then
317 if name
!= "Object" then
318 # it is a standard class without super class (but is not Object)
319 supertypes
.add objectclass
.mclass_type
320 else if kind
!= interface_kind
then
321 error
(nclassdef
, "Error: `Object` must be an {interface_kind}.")
329 # Check the validity of the specialization heirarchy
330 private fun check_supertypes
(nmodule
: AModule, nclassdef
: AClassdef)
332 var mmodule
= nmodule
.mmodule
333 if mmodule
== null then return
334 var mclass
= nclassdef
.mclass
335 if mclass
== null then return
336 var mclassdef
= nclassdef
.mclassdef
337 if mclassdef
== null then return
339 for s
in mclassdef
.supertypes
do
340 if s
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, mclassdef
.bound_mtype
) then
341 error
(nclassdef
, "Error: inheritance loop for class `{mclass}` with type `{s}`.")
346 # Build the classes of the module `nmodule`.
347 private fun build_classes
(nmodule
: AModule)
349 # Force building recursively
350 if nmodule
.build_classes_is_done
then return
351 nmodule
.build_classes_is_done
= true
352 var mmodule
= nmodule
.mmodule
353 if mmodule
== null then return
354 for imp
in mmodule
.in_importation
.direct_greaters
do
355 var nimp
= mmodule2node
(imp
)
356 if nimp
!= null then build_classes
(nimp
)
360 # process AStdClassdef before so that non-AStdClassdef classes can be attached to existing ones, if any
361 for nclassdef
in nmodule
.n_classdefs
do
362 if not nclassdef
isa AStdClassdef then continue
363 self.build_a_mclass
(nmodule
, nclassdef
)
365 for nclassdef
in nmodule
.n_classdefs
do
366 if nclassdef
isa AStdClassdef then continue
367 self.build_a_mclass
(nmodule
, nclassdef
)
370 # Create all classdefs
371 for nclassdef
in nmodule
.n_classdefs
do
372 if not nclassdef
isa AStdClassdef then continue
373 self.build_a_mclassdef
(nmodule
, nclassdef
)
375 for nclassdef
in nmodule
.n_classdefs
do
376 if nclassdef
isa AStdClassdef then continue
377 self.build_a_mclassdef
(nmodule
, nclassdef
)
380 # Create inheritance on all classdefs
381 for nclassdef
in nmodule
.n_classdefs
do
382 self.build_a_mclassdef_inheritance
(nmodule
, nclassdef
)
385 # Create the mclassdef hierarchy
386 for mclassdef
in mmodule
.mclassdefs
do
387 mclassdef
.add_in_hierarchy
391 for nclassdef
in nmodule
.n_classdefs
do
392 self.check_supertypes
(nmodule
, nclassdef
)
395 # Check unchecked ntypes
396 for nclassdef
in nmodule
.n_classdefs
do
397 if nclassdef
isa AStdClassdef then
398 var mclassdef
= nclassdef
.mclassdef
401 if mclassdef
== null then
405 mclass
= mclassdef
.mclass
406 anchor
= mclassdef
.bound_mtype
409 # check bound of formal parameter
410 for nfd
in nclassdef
.n_formaldefs
do
411 var nfdt
= nfd
.n_type
412 if nfdt
!= null and nfdt
.mtype
!= null then
413 var bound
= resolve_mtype3
(mmodule
, mclass
, anchor
, nfdt
)
414 if bound
== null then return # Forward error
417 # check declared super types
418 for nsc
in nclassdef
.n_superclasses
do
419 var ntype
= nsc
.n_type
420 if ntype
.mtype
!= null then
421 var mtype
= resolve_mtype3
(mmodule
, mclass
, anchor
, ntype
)
422 if mtype
== null then return # Forward error
428 # Check clash of ancestors
429 for nclassdef
in nmodule
.n_classdefs
do
430 var mclassdef
= nclassdef
.mclassdef
431 if mclassdef
== null then continue
432 var superclasses
= new HashMap[MClass, MClassType]
433 for scd
in mclassdef
.in_hierarchy
.greaters
do
434 for st
in scd
.supertypes
do
435 if not superclasses
.has_key
(st
.mclass
) then
436 superclasses
[st
.mclass
] = st
437 else if superclasses
[st
.mclass
] != st
then
438 var st1
= superclasses
[st
.mclass
].resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
439 var st2
= st
.resolve_for
(mclassdef
.mclass
.mclass_type
, mclassdef
.bound_mtype
, mmodule
, false)
441 error
(nclassdef
, "Error: incompatible ancestors for `{mclassdef.mclass}`; conflict: `{st1}` and `{st2}`")
448 # TODO: Check that the super-class is not intrusive
450 # Check that the superclasses are not already known (by transitivity)
451 for nclassdef
in nmodule
.n_classdefs
do
452 if not nclassdef
isa AStdClassdef or nclassdef
.is_broken
then continue
453 var mclassdef
= nclassdef
.mclassdef
454 if mclassdef
== null then continue
456 # Get the direct superclasses
457 # Since we are a mclassdef, just look at the mclassdef hierarchy
458 var parents
= new Array[MClass]
459 for sup
in mclassdef
.in_hierarchy
.direct_greaters
do
460 parents
.add
(sup
.mclass
)
463 # Used to track duplicates of superclasses
464 var seen_parents
= new ArrayMap[MClass, AType]
467 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
469 # Check each declared superclass to see if it belong to the direct superclass
470 for nsc
in nclassdef
.n_superclasses
do
471 var ntype
= nsc
.n_type
472 var mtype
= ntype
.mtype
474 # If the supertype is `null` or don’t refer to a class, we
475 # already raised an error.
476 if not mtype
isa MClassType then continue
478 var sc
= mtype
.mclass
479 if not parents
.has
(sc
) or sc
== objectclass
then
480 # Skip the warning on generated code
481 if ntype
.location
.file
!= null and not ntype
.location
.file
.filename
.is_empty
then
482 warning
(ntype
, "useless-superclass", "Warning: superfluous super-class `{mtype}` in class `{mclassdef.mclass}`.")
484 else if not seen_parents
.has_key
(sc
) then
485 seen_parents
[sc
] = ntype
487 warning
(ntype
, "useless-superclass", "Warning: duplicated super-class `{mtype}` in class `{mclassdef.mclass}`.")
493 # Registration of the nclassdef associated to each mclassdef
494 private var mclassdef2nclassdef
= new HashMap[MClassDef, AClassdef]
496 # Retrieve the associated AST node of a mclassdef.
498 # This method is used to associate model entity with syntactic entities.
499 # If the class definition is not associated with a node, returns `null`.
500 fun mclassdef2node
(mclassdef
: MClassDef): nullable AClassdef do
501 return mclassdef2nclassdef
.get_or_null
(mclassdef
)
506 # Flag that indicate if the class building is already completed
507 var build_classes_is_done
: Bool = false
508 # What is the AClassdef associated to a `MClass`?
509 # Used to check multiple definition of a class.
510 var mclass2nclassdef
: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
513 redef class AClassdef
514 # The associated MClass once build by a `ModelBuilder`
515 var mclass
: nullable MClass
516 # The associated MClassDef once build by a `ModelBuilder`
517 var mclassdef
: nullable MClassDef
518 # All (self and other) definitions for the same mclassdef
519 var all_defs
: nullable Array[AClassdef]
522 redef class AClasskind
523 # The class kind associated with the AST node class
524 private fun mkind
: MClassKind is abstract
526 redef class AConcreteClasskind
527 redef fun mkind
do return concrete_kind
529 redef class AAbstractClasskind
530 redef fun mkind
do return abstract_kind
532 redef class AInterfaceClasskind
533 redef fun mkind
do return interface_kind
535 redef class AEnumClasskind
536 redef fun mkind
do return enum_kind
538 redef class AExternClasskind
539 redef fun mkind
do return extern_kind
541 redef class ASubsetClasskind
542 redef fun mkind
do return subset_kind
545 redef class AFormaldef
546 # The associated parameter type
547 var mtype
: nullable MParameterType = null
549 # The associated bound
550 var bound
: nullable MType = null