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 else if nclassdef
isa ATopClassdef then
57 mkind
= interface_kind
59 mvisibility
= public_visibility
60 else if nclassdef
isa AMainClassdef then
65 mvisibility
= public_visibility
70 var mclass
= try_get_mclass_by_name
(nclassdef
, mmodule
, name
)
71 if mclass
== null then
72 mclass
= new MClass(mmodule
, name
, arity
, mkind
, mvisibility
)
73 #print "new class {mclass}"
74 else if nclassdef
isa AStdClassdef and nmodule
.mclass2nclassdef
.has_key
(mclass
) then
75 error
(nclassdef
, "Error: A class {name} is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
77 else if nclassdef
isa AStdClassdef and nclassdef
.n_kwredef
== null then
78 error
(nclassdef
, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
80 else if mclass
.arity
!= arity
then
81 error
(nclassdef
, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
83 else if nkind
!= null and mkind
!= concrete_kind
and mclass
.kind
!= mkind
then
84 error
(nkind
, "Error: refinement changed the kind from a {mclass.kind} to a {mkind}")
85 else if nvisibility
!= null and mvisibility
!= public_visibility
and mclass
.visibility
!= mvisibility
then
86 error
(nvisibility
, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
88 nclassdef
.mclass
= mclass
89 nmodule
.mclass2nclassdef
[mclass
] = nclassdef
92 # Visit the AST and create the MClassDef objects
93 private fun build_a_mclassdef
(nmodule
: AModule, nclassdef
: AClassdef)
95 var mmodule
= nmodule
.mmodule
.as(not null)
96 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
97 var mclass
= nclassdef
.mclass
98 if mclass
== null then return # Skip error
99 #var mclassdef = nclassdef.mclassdef.as(not null)
101 var names
= new Array[String]
102 var bounds
= new Array[MType]
103 if nclassdef
isa AStdClassdef and mclass
.arity
> 0 then
104 # Collect formal parameter names
105 for i
in [0..mclass
.arity
[ do
106 var nfd
= nclassdef
.n_formaldefs
[i
]
107 var ptname
= nfd
.n_id
.text
108 if names
.has
(ptname
) then
109 error
(nfd
, "Error: A formal parameter type `{ptname}' already exists")
115 # Revolve bound for formal parameter names
116 for i
in [0..mclass
.arity
[ do
117 var nfd
= nclassdef
.n_formaldefs
[i
]
118 var nfdt
= nfd
.n_type
120 var bound
= resolve_mtype_unchecked
(nclassdef
, nfdt
, false)
121 if bound
== null then return # Forward error
122 if bound
.need_anchor
then
124 error
(nfd
, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
128 else if mclass
.mclassdefs
.is_empty
then
129 # No bound, then implicitely bound by nullable Object
130 bounds
.add
(objectclass
.mclass_type
.as_nullable
)
133 bounds
.add
(mclass
.intro
.bound_mtype
.arguments
[i
])
138 var bound_mtype
= mclass
.get_mtype
(bounds
)
139 var mclassdef
= new MClassDef(mmodule
, bound_mtype
, nclassdef
.location
, names
)
140 nclassdef
.mclassdef
= mclassdef
141 self.mclassdef2nclassdef
[mclassdef
] = nclassdef
143 if mclassdef
.is_intro
then
144 self.toolcontext
.info
("{mclassdef} introduces new {mclass.kind} {mclass.full_name}", 3)
146 self.toolcontext
.info
("{mclassdef} refine {mclass.kind} {mclass.full_name}", 3)
150 # Visit the AST and set the super-types of the MClassdef objects
151 private fun collect_a_mclassdef_inheritance
(nmodule
: AModule, nclassdef
: AClassdef)
153 var mmodule
= nmodule
.mmodule
.as(not null)
154 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
155 var mclass
= nclassdef
.mclass
.as(not null)
156 var mclassdef
= nclassdef
.mclassdef
.as(not null)
158 var specobject
= true
159 var supertypes
= new Array[MClassType]
160 if nclassdef
isa AStdClassdef then
161 for nsc
in nclassdef
.n_superclasses
do
163 var ntype
= nsc
.n_type
164 var mtype
= resolve_mtype_unchecked
(nclassdef
, ntype
, false)
165 if mtype
== null then continue # Skip because of error
166 if not mtype
isa MClassType then
167 error
(ntype
, "Error: supertypes cannot be a formal type")
171 #print "new super : {mclass} < {mtype}"
174 if specobject
and mclass
.name
!= "Object" and objectclass
!= null and mclassdef
.is_intro
then
175 supertypes
.add objectclass
.mclass_type
178 mclassdef
.set_supertypes
(supertypes
)
179 if not supertypes
.is_empty
then self.toolcontext
.info
("{mclassdef} new super-types: {supertypes.join(", ")}", 3)
182 # Check the validity of the specialization heirarchy
183 private fun check_supertypes
(nmodule
: AModule, nclassdef
: AClassdef)
185 var mmodule
= nmodule
.mmodule
.as(not null)
186 var objectclass
= try_get_mclass_by_name
(nmodule
, mmodule
, "Object")
187 var mclass
= nclassdef
.mclass
.as(not null)
188 var mclassdef
= nclassdef
.mclassdef
.as(not null)
190 for s
in mclassdef
.supertypes
do
191 if s
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, mclassdef
.bound_mtype
) then
192 error
(nclassdef
, "Error: Inheritance loop for class {mclass} with type {s}")
197 # Build the classes of the module `nmodule'.
198 # REQUIRE: classes of imported modules are already build. (let `phase' do the job)
199 private fun build_classes
(nmodule
: AModule)
201 # Force building recursively
202 if nmodule
.build_classes_is_done
then return
203 nmodule
.build_classes_is_done
= true
204 var mmodule
= nmodule
.mmodule
.as(not null)
205 for imp
in mmodule
.in_importation
.direct_greaters
do
207 build_classes
(mmodule2nmodule
[imp
])
211 for nclassdef
in nmodule
.n_classdefs
do
212 self.build_a_mclass
(nmodule
, nclassdef
)
215 # Create all classdefs
216 for nclassdef
in nmodule
.n_classdefs
do
217 self.build_a_mclassdef
(nmodule
, nclassdef
)
220 for nclassdef
in nmodule
.n_classdefs
do
221 if nclassdef
.mclassdef
== null then return # forward error
224 # Create inheritance on all classdefs
225 for nclassdef
in nmodule
.n_classdefs
do
226 self.collect_a_mclassdef_inheritance
(nmodule
, nclassdef
)
229 # Create the mclassdef hierarchy
230 for nclassdef
in nmodule
.n_classdefs
do
231 var mclassdef
= nclassdef
.mclassdef
.as(not null)
232 mclassdef
.add_in_hierarchy
236 for nclassdef
in nmodule
.n_classdefs
do
237 self.check_supertypes
(nmodule
, nclassdef
)
240 # Check unchecked ntypes
241 for nclassdef
in nmodule
.n_classdefs
do
242 if nclassdef
isa AStdClassdef then
243 # check bound of formal parameter
244 for nfd
in nclassdef
.n_formaldefs
do
245 var nfdt
= nfd
.n_type
246 if nfdt
!= null and nfdt
.mtype
!= null then
247 var bound
= resolve_mtype
(nclassdef
, nfdt
)
248 if bound
== null then return # Forward error
251 # check declared super types
252 for nsc
in nclassdef
.n_superclasses
do
253 var ntype
= nsc
.n_type
254 if ntype
.mtype
!= null then
255 var mtype
= resolve_mtype
(nclassdef
, ntype
)
256 if mtype
== null then return # Forward error
263 # TODO: Check that the super-class is not intrusive
265 # TODO: Check that the super-class is not already known (by transitivity)
268 # Register the nclassdef associated to each mclassdef
269 # FIXME: why not refine the MClassDef class with a nullable attribute?
270 var mclassdef2nclassdef
: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
272 # Return the static type associated to the node `ntype'.
273 # `classdef' is the context where the call is made (used to understand formal types)
274 # The mmodule used as context is `nclassdef.mmodule'
275 # In case of problem, an error is displayed on `ntype' and null is returned.
276 # FIXME: the name "resolve_mtype" is awful
277 fun resolve_mtype_unchecked
(nclassdef
: AClassdef, ntype
: AType, with_virtual
: Bool): nullable MType
279 var name
= ntype
.n_id
.text
280 var mclassdef
= nclassdef
.mclassdef
281 var mmodule
= nclassdef
.parent
.as(AModule).mmodule
.as(not null)
285 if mclassdef
!= null and with_virtual
then
286 var prop
= try_get_mproperty_by_name
(ntype
, mclassdef
, name
).as(nullable MVirtualTypeProp)
288 if not ntype
.n_types
.is_empty
then
289 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
291 res
= prop
.mvirtualtype
292 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
298 # Check parameter type
299 if mclassdef
!= null and mclassdef
.parameter_names
.has
(name
) then
300 if not ntype
.n_types
.is_empty
then
301 error
(ntype
, "Type error: formal type {name} cannot have formal parameters.")
303 for i
in [0..mclassdef
.parameter_names
.length
[ do
304 if mclassdef
.parameter_names
[i
] == name
then
305 res
= mclassdef
.mclass
.mclass_type
.arguments
[i
]
306 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
315 var mclass
= try_get_mclass_by_name
(ntype
, mmodule
, name
)
316 if mclass
!= null then
317 var arity
= ntype
.n_types
.length
318 if arity
!= mclass
.arity
then
320 error
(ntype
, "Type error: '{name}' is a generic class.")
321 else if mclass
.arity
== 0 then
322 error
(ntype
, "Type error: '{name}' is not a generic class.")
324 error
(ntype
, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
329 res
= mclass
.mclass_type
330 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
334 var mtypes
= new Array[MType]
335 for nt
in ntype
.n_types
do
336 var mt
= resolve_mtype_unchecked
(nclassdef
, nt
, with_virtual
)
337 if mt
== null then return null # Forward error
340 res
= mclass
.get_mtype
(mtypes
)
341 if ntype
.n_kwnullable
!= null then res
= res
.as_nullable
347 # If everything fail, then give up :(
348 error
(ntype
, "Type error: class {name} not found in module {mmodule}.")
352 # Return the static type associated to the node `ntype'.
353 # `classdef' is the context where the call is made (used to understand formal types)
354 # The mmodule used as context is `nclassdef.mmodule'
355 # In case of problem, an error is displayed on `ntype' and null is returned.
356 # FIXME: the name "resolve_mtype" is awful
357 fun resolve_mtype
(nclassdef
: AClassdef, ntype
: AType): nullable MType
359 var mtype
= ntype
.mtype
360 if mtype
== null then mtype
= resolve_mtype_unchecked
(nclassdef
, ntype
, true)
361 if mtype
== null then return null # Forward error
363 if ntype
.checked_mtype
then return mtype
364 if mtype
isa MGenericType then
365 var mmodule
= nclassdef
.parent
.as(AModule).mmodule
.as(not null)
366 var mclassdef
= nclassdef
.mclassdef
367 var mclass
= mtype
.mclass
368 for i
in [0..mclass
.arity
[ do
369 var bound
= mclass
.intro
.bound_mtype
.arguments
[i
]
370 var nt
= ntype
.n_types
[i
]
371 var mt
= resolve_mtype
(nclassdef
, nt
)
372 if mt
== null then return null # forward error
373 if not mt
.is_subtype
(mmodule
, mclassdef
.bound_mtype
, bound
) then
374 error
(nt
, "Type error: expected {bound}, got {mt}")
379 ntype
.checked_mtype
= true
386 # Flag that indicate if the class building is already completed
387 var build_classes_is_done
: Bool = false
388 # What is the AClassdef associated to a MClass?
389 # Used to check multiple definition of a class.
390 var mclass2nclassdef
: Map[MClass, AClassdef] = new HashMap[MClass, AClassdef]
393 redef class AClassdef
394 # The associated MClass once build by a `ModelBuilder'
395 var mclass
: nullable MClass
396 # The associated MClassDef once build by a `ModelBuilder'
397 var mclassdef
: nullable MClassDef
400 redef class AClasskind
401 # The class kind associated with the AST node class
402 private fun mkind
: MClassKind is abstract
404 redef class AConcreteClasskind
405 redef fun mkind
do return concrete_kind
407 redef class AAbstractClasskind
408 redef fun mkind
do return abstract_kind
410 redef class AInterfaceClasskind
411 redef fun mkind
do return interface_kind
413 redef class AEnumClasskind
414 redef fun mkind
do return enum_kind
416 redef class AExternClasskind
417 redef fun mkind
do return extern_kind
421 # The mtype associated to the node
422 var mtype
: nullable MType = null
424 # Is the mtype a valid one?
425 var checked_mtype
: Bool = false