1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # Static types and property signatures
21 intrude import abstractmetamodel
23 redef class MMLocalClass
24 # Cached result of get_type
25 var _base_type_cache
: nullable MMType
27 # Return the type of self for this class
30 if _base_type_cache
== null then _base_type_cache
= new MMTypeSimpleClass(self)
31 return _base_type_cache
.as(not null)
34 # Register a new ancestor
35 protected fun add_ancestor
(a
: MMAncestor)
37 assert not _ancestors
.has_key
(a
.local_class
)
38 assert a
.local_class
!= self
39 _ancestors
[a
.local_class
] = a
42 # Array of ancestor that associate each superclass with the corresponding ancestor
43 readable var _ancestors
: nullable Map[MMLocalClass, MMAncestor]
45 # The ancestor type for a given superclass
46 fun ancestor
(c
: MMLocalClass): MMType
48 return _ancestors
[c
].stype
52 redef class MMLocalProperty
53 # The signature of the property (where it is declared)
54 readable writable var _signature
: nullable MMSignature
56 var _signatures_cache
: HashMap[MMType, MMSignature] = new HashMap[MMType, MMSignature]
58 # Return the adapted signature of self for a receiver of type t
59 fun signature_for
(t
: MMType): MMSignature do
60 if t
== local_class
.get_type
then return signature
.as(not null)
62 if _signatures_cache
.has_key
(t
) then return _signatures_cache
[t
]
64 var res
= signature
.adaptation_to
(t
)
65 _signatures_cache
[t
] = res
70 # Signature for local properties
72 # The type of the reveiver
73 readable var _recv
: MMType
76 var _params
: Array[MMType]
79 readable var _return_type
: nullable MMType
81 # The closure parameters
82 readable var _closures
: Array[MMClosure] = new Array[MMClosure]
84 # Return the closure named 'name'. Null if no such a closure exists.
85 fun closure_named
(name
: Symbol): nullable MMClosure
88 if c
.name
== name
then return c
93 # Number of parameters
99 # Is self a valid subtype of an other signature
100 fun <(s
: MMSignature): Bool
105 assert _recv
.mmmodule
== s
.recv
.mmmodule
106 var rt
= _return_type
107 var srt
= s
.return_type
108 if arity
!= s
.arity
or (rt
== null) != (srt
== null) then return false
109 if rt
!= null and not rt
< srt
.as(not null) then
113 for i
in [0..arity
[ do
114 if not s
[i
] < self[i
] then
119 if closures
.length
!= s
.closures
.length
then return false
120 for i
in [0..closures
.length
[ do
121 if not s
.closures
[i
] < closures
[i
] then return false
126 # The type of the i-th parameter
127 fun [](i
: Int): MMType
129 assert _params
.length
> i
136 if _params
.length
> 0 then
138 var a
= new Array[String].with_capacity
(_params
.length
)
139 for i
in [0.._params
.length
[ do
140 #var pn = _params_name[i]
145 s
.append
("({a.join(",")})")
147 var rt
= _return_type
148 if rt
!= null then s
.append
(": {rt}")
152 # Adapt the signature to a different receiver
153 fun adaptation_to
(r
: MMType): MMSignature
159 var p
= new Array[MMType]
161 p
.add
(i
.for_module
(mod
).adapt_to
(r
))
163 var rv
= _return_type
165 rv
= rv
.for_module
(mod
).adapt_to
(r
)
167 var res
= new MMSignature(p
,rv
,r
)
168 for clos
in _closures
do
169 res
.closures
.add
(clos
.adaptation_to
(r
))
174 var _not_for_self_cache
: nullable MMSignature = null
176 # Return a type approximation if the reveiver is not self
177 # Useful for virtual types
178 fun not_for_self
: MMSignature
180 if _not_for_self_cache
!= null then return _not_for_self_cache
.as(not null)
182 var need_for_self
= false
183 var p
= new Array[MMType]
185 var i2
= i
.not_for_self
186 if i
!= i2
then need_for_self
= true
190 var rv
= _return_type
193 if rv
!= _return_type
then need_for_self
= true
196 var clos
= new Array[MMClosure]
197 for c
in _closures
do
198 var c2
= c
.not_for_self
199 if c2
!= c
then need_for_self
= true
204 if need_for_self
then
205 res
= new MMSignature(p
, rv
, _recv
)
206 res
.closures
.add_all
(clos
)
211 _not_for_self_cache
= res
215 init(params
: Array[MMType], return_type
: nullable MMType, r
: MMType)
218 _return_type
= return_type
223 # A closure in a signature
225 # The name of the closure (without the !)
226 readable var _name
: Symbol
228 # The signature of the closure
229 readable var _signature
: MMSignature
231 # Is the closure a brek one
232 # aka is defined with the break keyword thus does not return
233 readable var _is_break
: Bool
235 # Is the closure optional?
236 # ie is there a default definition
237 readable var _is_optional
: Bool
239 # Adapt the signature to a different receiver
240 fun adaptation_to
(r
: MMType): MMClosure
242 return new MMClosure(_name
, _signature
.adaptation_to
(r
), _is_break
, _is_optional
)
245 init(name
: Symbol, s
: MMSignature, is_break
: Bool, is_optional
: Bool)
250 _is_optional
= is_optional
253 fun not_for_self
: MMClosure
255 var sig
= _signature
.not_for_self
256 if sig
!= _signature
then
257 return new MMClosure(_name
, sig
, _is_break
, _is_optional
)
263 fun <(c
: MMClosure): Bool
265 if c
.is_optional
and not is_optional
then return false
266 if not c
.is_break
and is_break
then return false
267 return c
.signature
< signature
271 # Inheritance relation between two types
272 abstract class MMAncestor
274 writable var _stype
: nullable MMType = null
277 fun stype
: MMType do return _stype
.as(not null)
279 # The inheriter (heir) type
280 writable var _inheriter
: nullable MMType = null
282 # The inheriter (heir) type
283 fun inheriter
: MMType do return _inheriter
.as(not null)
285 fun is_reffinement
: Bool do
286 return stype
.mmmodule
!= stype
.mmmodule
289 fun is_specialisation
: Bool do
290 return stype
.local_class
.global
!= inheriter
.local_class
.global
293 # The inherited class
294 fun local_class
: MMLocalClass is abstract
298 if _stype
== null then
299 return local_class
.to_s
307 # Note that static type a related to a specific module
308 abstract class MMType
309 # The module where self makes sence
310 fun mmmodule
: MMModule is abstract
312 # The local class that self direclty or indirectly refers to
313 fun local_class
: MMLocalClass is abstract
315 # Is the type a valid one
316 # For instance, circular dependency on formal types is invalid
317 fun is_valid
: Bool do return true
319 # Is self a valid subtype of t
320 fun <(t
: MMType): Bool is abstract
322 # Is self a valid supertype of t
323 # This method must be only called within definition of < if
324 # a double dispatch is needed
325 fun is_supertype
(t
: MMType): Bool is abstract
327 # Adapt self to another module
328 fun for_module
(mod
: MMModule): MMType is abstract
330 # Get the type adapted to another receiver type
331 # Useful for formal types
332 fun adapt_to
(recv
: MMType): MMType is abstract
334 # Adapt self to another local class context
335 # Useful for genericity
336 # 'c' Must be a super-class of self
339 # class B[F] special A[F]
340 # class C[G] special B[String]
341 # class D special C[Float]
342 # 'A[Int]'.upcast_for('A') -> 'A[Int]'
343 # 'A[Int]'.upcast_for('B') -> abort
344 # 'B[Int]'.upcast_for('B') -> 'B[Int]'
345 # 'B[Int]'.upcast_for('A') -> 'A[Int]'
346 # 'B[Int]'.upcast_for('C') -> abort
347 # 'C[Int]'.upcast_for('C') -> 'C[Int]'
348 # 'C[Int]'.upcast_for('B') -> 'B[String]'
349 # 'C[Int]'.upcast_for('A') -> 'A[String]'
350 # 'D'.upcast_for('D') -> 'D'
351 # 'D'.upcast_for('C') -> 'C[Float]'
352 # 'D'.upcast_for('B') -> 'C[String]'
353 # 'D'.upcast_for('A') -> 'A[String]'
354 fun upcast_for
(c
: MMLocalClass): MMType is abstract
356 # Return a type approximation if the reveiver is not self
357 # Useful for virtual types
358 fun not_for_self
: MMType do return self
360 # The nullable version of self (if needed)
361 var _as_nullable_cache
: nullable MMType = null
363 # IS the type can accept null?
364 fun is_nullable
: Bool do return false
366 # Return the nullable version of the type
367 # Noop if already nullable
368 fun as_nullable
: MMType do
369 var cache
= _as_nullable_cache
370 if cache
!= null then return cache
371 var res
= new MMNullableType(self)
372 _as_nullable_cache
= res
376 # Return the not null version of the type
377 # Noop if already not null
378 fun as_notnull
: MMType do return self
383 var _base_type
: MMType
384 redef fun is_valid
do return _base_type
.is_valid
385 redef fun is_nullable
: Bool do return true
386 redef fun as_notnull
do return _base_type
387 redef fun as_nullable
do return self
388 init(t
: MMType) do _base_type
= t
390 redef fun mmmodule
do return _base_type
.mmmodule
392 redef fun local_class
do return _base_type
.local_class
396 return t
isa MMNullableType and _base_type
< t
.as_notnull
401 return "nullable {_base_type}"
404 redef fun is_supertype
(t
)
406 return _base_type
.is_supertype
(t
)
409 redef fun for_module
(mod
)
411 return _base_type
.for_module
(mod
).as_nullable
414 redef fun adapt_to
(recv
)
416 return _base_type
.adapt_to
(recv
).as_nullable
419 redef fun upcast_for
(c
)
421 return _base_type
.upcast_for
(c
)
424 redef fun not_for_self
426 return _base_type
.not_for_self
.as_nullable
432 redef readable var _local_class
: MMLocalClass
433 redef fun mmmodule
do return _local_class
.mmmodule
end
434 redef fun <(t
) do return t
.is_supertype
(self)
438 return _local_class
.to_s
441 redef fun upcast_for
(c
)
444 if _local_class
!= c
then
445 t
= _local_class
.ancestor
(c
)
450 init(c
: MMLocalClass)
456 class MMTypeSimpleClass
458 redef fun is_supertype
(t
)
460 return t
.local_class
.cshe
<= _local_class
463 redef fun for_module
(mod
)
466 if mmmodule
!= mod
then
467 t
= _local_class
.for_module
(mod
).get_type
472 redef fun adapt_to
(recv
) do return self
474 init(c
: MMLocalClass)
483 redef readable var _mmmodule
: MMModule
484 redef fun is_nullable
: Bool do return true
485 redef fun <(t
) do return t
isa MMTypeNone or t
isa MMNullableType
486 redef fun to_s
do return "null"
487 redef fun is_supertype
(t
) do return false
488 redef fun local_class
do abort
489 redef fun upcast_for
(c
) do abort
490 redef fun as_nullable
do return self
491 redef fun as_notnull
do abort
493 private init(m
: MMModule) do _mmmodule
= m
498 readable var _type_none
: MMTypeNone = new MMTypeNone(self)
501 fun type_bool
: MMType
503 return class_by_name
(once
("Bool".to_symbol
)).get_type