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
23 redef class ToolContext
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 if nclassdef
isa AStdClassdef then
49 name
= nclassdef
.n_id
.text
50 nkind
= nclassdef
.n_classkind
52 nvisibility
= nclassdef
.n_visibility
53 mvisibility
= nvisibility
.mvisibility
54 arity
= nclassdef
.n_formaldefs
.length
55 else if nclassdef
isa ATopClassdef then
58 mkind
= interface_kind
60 mvisibility
= public_visibility
61 else if nclassdef
isa AMainClassdef then
66 mvisibility
= public_visibility
71 var mclass
= try_get_mclass_by_name
(nclassdef
, mmodule
, name
)
72 if mclass
== null then
73 mclass
= new MClass(mmodule
, name
, arity
, mkind
, mvisibility
)
74 #print "new class {mclass}"
75 else if nclassdef
isa AStdClassdef and nmodule
.mclass2nclassdef
.has_key
(mclass
) then
76 error
(nclassdef
, "Error: A class {name} is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
78 else if nclassdef
isa AStdClassdef and nclassdef
.n_kwredef
== null then
79 error
(nclassdef
, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
81 else if mclass
.arity
!= arity
then
82 error
(nclassdef
, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
84 else if nkind
!= null and mkind
!= concrete_kind
and mclass
.kind
!= mkind
then
85 error
(nkind
, "Error: refinement changed the kind from a {mclass.kind} to a {mkind}")
86 else if nvisibility
!= null and mvisibility
!= public_visibility
and mclass
.visibility
!= mvisibility
then
87 error
(nvisibility
, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
89 nclassdef
.mclass
= mclass
90 nmodule
.mclass2nclassdef
[mclass
] = nclassdef
93 # Visit the AST and create the MClassDef objects
94 private fun build_a_mclassdef
(nmodule
: AModule, nclassdef
: AClassdef)
96 var mmodule
= nmodule
.mmodule
.as(not null)
97 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
98 var mclass
= nclassdef
.mclass
99 if mclass
== null then return # Skip error
100 #var mclassdef = nclassdef.mclassdef.as(not null)
102 var names
= new Array[String]
103 var bounds
= new Array[MType]
104 if nclassdef
isa AStdClassdef and mclass
.arity
> 0 then
105 # Collect formal parameter names
106 for i
in [0..mclass
.arity
[ do
107 var nfd
= nclassdef
.n_formaldefs
[i
]
108 var ptname
= nfd
.n_id
.text
109 if names
.has
(ptname
) then
110 error
(nfd
, "Error: A formal parameter type `{ptname}' already exists")
116 # Revolve bound for formal parameter names
117 for i
in [0..mclass
.arity
[ do
118 var nfd
= nclassdef
.n_formaldefs
[i
]
119 var nfdt
= nfd
.n_type
121 var bound
= resolve_mtype_unchecked
(nclassdef
, nfdt
, false)
122 if bound
== null then return # Forward error
123 if bound
.need_anchor
then
125 error
(nfd
, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
129 else if mclass
.mclassdefs
.is_empty
then
130 # No bound, then implicitely bound by nullable Object
131 bounds
.add
(objectclass
.mclass_type
.as_nullable
)
134 bounds
.add
(mclass
.intro
.bound_mtype
.arguments
[i
])
139 var bound_mtype
= mclass
.get_mtype
(bounds
)
140 var mclassdef
= new MClassDef(mmodule
, bound_mtype
, nclassdef
.location
, names
)
141 nclassdef
.mclassdef
= mclassdef
142 self.mclassdef2nclassdef
[mclassdef
] = nclassdef
144 if mclassdef
.is_intro
then
145 self.toolcontext
.info
("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
147 self.toolcontext
.info
("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
151 # Visit the AST and set the super-types of the MClassdef objects
152 private fun collect_a_mclassdef_inheritance
(nmodule
: AModule, nclassdef
: AClassdef)
154 var mmodule
= nmodule
.mmodule
.as(not null)
155 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
156 var mclass
= nclassdef
.mclass
.as(not null)
157 var mclassdef
= nclassdef
.mclassdef
.as(not null)
159 var specobject
= true
160 var supertypes
= new Array[MClassType]
161 if nclassdef
isa AStdClassdef then
162 for nsc
in nclassdef
.n_superclasses
do
164 var ntype
= nsc
.n_type
165 var mtype
= resolve_mtype_unchecked
(nclassdef
, ntype
, false)
166 if mtype
== null then continue # Skip because of error
167 if not mtype
isa MClassType then
168 error
(ntype
, "Error: supertypes cannot be a formal type")
172 #print "new super : {mclass} < {mtype}"
175 if specobject
and mclass
.name
!= "Object" and objectclass
!= null and mclassdef
.is_intro
then
176 supertypes
.add objectclass
.mclass_type
179 mclassdef
.set_supertypes
(supertypes
)
180 if not supertypes
.is_empty
then self.toolcontext
.info
("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
183 # Check the validity of the specialization heirarchy
184 private fun check_supertypes
(nmodule
: AModule, nclassdef
: AClassdef)
186 var mmodule
= nmodule
.mmodule
.as(not null)
187 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
188 var mclass
= nclassdef
.mclass
.as(not null)
189 var mclassdef
= nclassdef
.mclassdef
.as(not null)
191 for s
in mclassdef
.supertypes
do
192 if s
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, mclassdef
.bound_mtype
) then
193 error
(nclassdef
, "Error: Inheritance loop for class {mclass} with type {s}")
198 # Build the classes of the module `nmodule'.
199 # REQUIRE: classes of imported modules are already build. (let `phase' do the job)
200 private fun build_classes
(nmodule
: AModule)
202 # Force building recursively
203 if nmodule
.build_classes_is_done
then return
204 nmodule
.build_classes_is_done
= true
205 var mmodule
= nmodule
.mmodule
.as(not null)
206 for imp
in mmodule
.in_importation
.direct_greaters
do
208 build_classes
(mmodule2nmodule
[imp
])
212 for nclassdef
in nmodule
.n_classdefs
do
213 self.build_a_mclass
(nmodule
, nclassdef
)
216 # Create all classdefs
217 for nclassdef
in nmodule
.n_classdefs
do
218 self.build_a_mclassdef
(nmodule
, nclassdef
)
221 for nclassdef
in nmodule
.n_classdefs
do
222 if nclassdef
.mclassdef
== null then return # forward error
225 # Create inheritance on all classdefs
226 for nclassdef
in nmodule
.n_classdefs
do
227 self.collect_a_mclassdef_inheritance
(nmodule
, nclassdef
)
230 # Create the mclassdef hierarchy
231 for nclassdef
in nmodule
.n_classdefs
do
232 var mclassdef
= nclassdef
.mclassdef
.as(not null)
233 mclassdef
.add_in_hierarchy
237 for nclassdef
in nmodule
.n_classdefs
do
238 self.check_supertypes
(nmodule
, nclassdef
)
241 # Check unchecked ntypes
242 for nclassdef
in nmodule
.n_classdefs
do
243 if nclassdef
isa AStdClassdef then
244 # check bound of formal parameter
245 for nfd
in nclassdef
.n_formaldefs
do
246 var nfdt
= nfd
.n_type
247 if nfdt
!= null and nfdt
.mtype
!= null then
248 var bound
= resolve_mtype
(nclassdef
, nfdt
)
249 if bound
== null then return # Forward error
252 # check declared super types
253 for nsc
in nclassdef
.n_superclasses
do
254 var ntype
= nsc
.n_type
255 if ntype
.mtype
!= null then
256 var mtype
= resolve_mtype
(nclassdef
, ntype
)
257 if mtype
== null then return # Forward error
264 # TODO: Check that the super-class is not intrusive
266 # TODO: Check that the super-class is not already known (by transitivity)
269 # Register the nclassdef associated to each mclassdef
270 # FIXME: why not refine the MClassDef class with a nullable attribute?
271 var mclassdef2nclassdef
: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
273 # Return the static type associated to the node `ntype'.
274 # `classdef' is the context where the call is made (used to understand formal types)
275 # The mmodule used as context is `nclassdef.mmodule'
276 # In case of problem, an error is displayed on `ntype' and null is returned.
277 # FIXME: the name "resolve_mtype" is awful
278 fun resolve_mtype_unchecked
(nclassdef
: AClassdef, ntype
: AType, with_virtual
: Bool): nullable MType
280 var name
= ntype
.n_id
.text
281 var mclassdef
= nclassdef
.mclassdef
282 var mmodule
= nclassdef
.parent
.as(AModule).mmodule
.as(not null)
286 if mclassdef
!= null and with_virtual
then
287 var prop
= try_get_mproperty_by_name
(ntype
, mclassdef
, name
).as(nullable MVirtualTypeProp)
289 if not ntype
.n_types
.is_empty
then
290 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
292 res
= prop
.mvirtualtype
293 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
299 # Check parameter type
300 if mclassdef
!= null and mclassdef
.parameter_names
.has
(name
) then
301 if not ntype
.n_types
.is_empty
then
302 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
304 for i
in [0..mclassdef
.parameter_names
.length
[ do
305 if mclassdef
.parameter_names
[i
] == name
then
306 res
= mclassdef
.mclass
.mclass_type
.arguments
[i
]
307 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
316 var mclass
= try_get_mclass_by_name
(ntype
, mmodule
, name
)
317 if mclass
!= null then
318 var arity
= ntype
.n_types
.length
319 if arity
!= mclass
.arity
then
321 error
(ntype
, "Type error: '{name}' is a generic class.")
322 else if mclass
.arity
== 0 then
323 error
(ntype
, "Type error: '{name}' is not a generic class.")
325 error
(ntype
, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
330 res
= mclass
.mclass_type
331 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
335 var mtypes
= new Array[MType]
336 for nt
in ntype
.n_types
do
337 var mt
= resolve_mtype_unchecked
(nclassdef
, nt
, with_virtual
)
338 if mt
== null then return null # Forward error
341 res
= mclass
.get_mtype
(mtypes
)
342 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
348 # If everything fail, then give up :(
349 error
(ntype
, "Type error: class {name} not found in module {mmodule}.")
353 # Return the static type associated to the node `ntype'.
354 # `classdef' is the context where the call is made (used to understand formal types)
355 # The mmodule used as context is `nclassdef.mmodule'
356 # In case of problem, an error is displayed on `ntype' and null is returned.
357 # FIXME: the name "resolve_mtype" is awful
358 fun resolve_mtype
(nclassdef
: AClassdef, ntype
: AType): nullable MType
360 var mtype
= ntype
.mtype
361 if mtype
== null then mtype
= resolve_mtype_unchecked
(nclassdef
, ntype
, true)
362 if mtype
== null then return null # Forward error
364 if ntype
.checked_mtype
then return mtype
365 if mtype
isa MGenericType then
366 var mmodule
= nclassdef
.parent
.as(AModule).mmodule
.as(not null)
367 var mclassdef
= nclassdef
.mclassdef
368 var mclass
= mtype
.mclass
369 for i
in [0..mclass
.arity
[ do
370 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
371 var nt
= ntype
.n_types
[i
]
372 var mt
= resolve_mtype
(nclassdef
, nt
)
373 if mt
== null then return null # forward error
374 if not mt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, bound
) then
375 error
(nt
, "Type error: expected {bound}, got {mt}")
380 ntype
.checked_mtype
= true
387 # Flag that indicate if the class building is already completed
388 var build_classes_is_done
: Bool = false
389 # What is the AClassdef associated to a MClass?
390 # Used to check multiple definition of a class.
391 var mclass2nclassdef
: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
394 redef class AClassdef
395 # The associated MClass once build by a `ModelBuilder'
396 var mclass
: nullable MClass
397 # The associated MClassDef once build by a `ModelBuilder'
398 var mclassdef
: nullable MClassDef
401 redef class AClasskind
402 # The class kind associated with the AST node class
403 private fun mkind
: MClassKind is abstract
405 redef class AConcreteClasskind
406 redef fun mkind
do return concrete_kind
408 redef class AAbstractClasskind
409 redef fun mkind
do return abstract_kind
411 redef class AInterfaceClasskind
412 redef fun mkind
do return interface_kind
414 redef class AEnumClasskind
415 redef fun mkind
do return enum_kind
417 redef class AExternClasskind
418 redef fun mkind
do return extern_kind
422 # The mtype associated to the node
423 var mtype
: nullable MType = null
425 # Is the mtype a valid one?
426 var checked_mtype
: Bool = false