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
71 readable var _mmtype
: MMType
72 readable var _name
: Symbol
74 init ( t
: MMType, n
: Symbol )
80 redef fun to_s
do return "{name}: {mmtype}"
83 # Signature for local properties
85 # The type of the reveiver
86 readable var _recv
: MMType
89 readable var _params
: Array[MMParam]
92 readable var _return_type
: nullable MMType
94 # The closure parameters
95 readable var _closures
: Array[MMClosure] = new Array[MMClosure]
97 # Return the closure named 'name'. Null if no such a closure exists.
98 fun closure_named
(name
: Symbol): nullable MMClosure
100 for c
in _closures
do
101 if c
.name
== name
then return c
106 # Number of parameters
109 return _params
.length
112 # Is self a valid subtype of an other signature
113 fun <(s
: MMSignature): Bool
118 assert _recv
.mmmodule
== s
.recv
.mmmodule
119 var rt
= _return_type
120 var srt
= s
.return_type
121 if arity
!= s
.arity
or (rt
== null) != (srt
== null) then return false
122 if rt
!= null and not rt
< srt
.as(not null) then
126 for i
in [0..arity
[ do
127 if not s
[i
] < self[i
] then
132 if closures
.length
!= s
.closures
.length
then return false
133 for i
in [0..closures
.length
[ do
134 if not s
.closures
[i
] < closures
[i
] then return false
139 # The type of the i-th parameter
140 fun [](i
: Int): MMType
142 assert _params
.length
> i
143 return _params
[i
].mmtype
149 if _params
.length
> 0 then
151 var a
= new Array[String].with_capacity
(_params
.length
)
152 for i
in [0.._params
.length
[ do
156 s
.append
("({a.join(", ")})")
158 var rt
= _return_type
159 if rt
!= null then s
.append
(": {rt}")
163 # Adapt the signature to a different receiver
164 fun adaptation_to
(r
: MMType): MMSignature
170 var p
= new Array[MMParam]
172 var new_type
= i
.mmtype
.for_module
(mod
).adapt_to
(r
)
174 if new_type
== i
.mmtype
then
177 new_param
= new MMParam( new_type
, i
.name
)
182 var rv
= _return_type
184 rv
= rv
.for_module
(mod
).adapt_to
(r
)
186 var res
= new MMSignature(p
,rv
,r
)
187 for clos
in _closures
do
188 res
.closures
.add
(clos
.adaptation_to
(r
))
193 var _not_for_self_cache
: nullable MMSignature = null
195 # Return a type approximation if the reveiver is not self
196 # Useful for virtual types
197 fun not_for_self
: MMSignature
199 if _not_for_self_cache
!= null then return _not_for_self_cache
.as(not null)
201 var need_for_self
= false
202 var p
= new Array[MMParam]
204 var new_type
= i
.mmtype
.not_for_self
206 if i
.mmtype
== new_type
then
210 new_param
= new MMParam( new_type
, i
.name
)
216 var rv
= _return_type
219 if rv
!= _return_type
then need_for_self
= true
222 var clos
= new Array[MMClosure]
223 for c
in _closures
do
224 var c2
= c
.not_for_self
225 if c2
!= c
then need_for_self
= true
230 if need_for_self
then
231 res
= new MMSignature(p
, rv
, _recv
)
232 res
.closures
.add_all
(clos
)
237 _not_for_self_cache
= res
241 init(params
: Array[MMParam], return_type
: nullable MMType, r
: MMType)
244 _return_type
= return_type
249 # A closure in a signature
251 # The name of the closure (without the !)
252 readable var _name
: Symbol
254 # The signature of the closure
255 readable var _signature
: MMSignature
257 # Is the closure a brek one
258 # aka is defined with the break keyword thus does not return
259 readable var _is_break
: Bool
261 # Is the closure optional?
262 # ie is there a default definition
263 readable var _is_optional
: Bool
265 # Adapt the signature to a different receiver
266 fun adaptation_to
(r
: MMType): MMClosure
268 return new MMClosure(_name
, _signature
.adaptation_to
(r
), _is_break
, _is_optional
)
271 init(name
: Symbol, s
: MMSignature, is_break
: Bool, is_optional
: Bool)
276 _is_optional
= is_optional
279 fun not_for_self
: MMClosure
281 var sig
= _signature
.not_for_self
282 if sig
!= _signature
then
283 return new MMClosure(_name
, sig
, _is_break
, _is_optional
)
289 fun <(c
: MMClosure): Bool
291 if c
.is_optional
and not is_optional
then return false
292 if not c
.is_break
and is_break
then return false
293 return c
.signature
< signature
297 # Inheritance relation between two types
298 abstract class MMAncestor
300 writable var _stype
: nullable MMType = null
303 fun stype
: MMType do return _stype
.as(not null)
305 # The inheriter (heir) type
306 writable var _inheriter
: nullable MMType = null
308 # The inheriter (heir) type
309 fun inheriter
: MMType do return _inheriter
.as(not null)
311 fun is_reffinement
: Bool do
312 return stype
.mmmodule
!= stype
.mmmodule
315 fun is_specialisation
: Bool do
316 return stype
.local_class
.global
!= inheriter
.local_class
.global
319 # The inherited class
320 fun local_class
: MMLocalClass is abstract
324 if _stype
== null then
325 return local_class
.to_s
333 # Note that static type is related to a specific module
334 abstract class MMType
335 # The module where self makes sence
336 fun mmmodule
: MMModule is abstract
338 # The local class that self direclty or indirectly refers to
339 fun local_class
: MMLocalClass is abstract
341 # Is the type a valid one
342 # For instance, circular dependency on formal types is invalid
343 fun is_valid
: Bool do return true
345 # Is self a valid subtype of t
346 fun <(t
: MMType): Bool is abstract
348 # Is self a valid supertype of t
349 # This method must be only called within definition of < if
350 # a double dispatch is needed
351 fun is_supertype
(t
: MMType): Bool is abstract
353 # Adapt self to another module
354 fun for_module
(mod
: MMModule): MMType is abstract
356 # Get the type adapted to another receiver type
357 # Useful for formal types
358 fun adapt_to
(recv
: MMType): MMType is abstract
360 # Adapt self to another local class context
361 # Useful for genericity
362 # 'c' Must be a super-class of self
365 # class B[F] super A[F]
366 # class C[G] super B[String]
367 # class D super C[Float]
368 # 'A[Int]'.upcast_for('A') -> 'A[Int]'
369 # 'A[Int]'.upcast_for('B') -> abort
370 # 'B[Int]'.upcast_for('B') -> 'B[Int]'
371 # 'B[Int]'.upcast_for('A') -> 'A[Int]'
372 # 'B[Int]'.upcast_for('C') -> abort
373 # 'C[Int]'.upcast_for('C') -> 'C[Int]'
374 # 'C[Int]'.upcast_for('B') -> 'B[String]'
375 # 'C[Int]'.upcast_for('A') -> 'A[String]'
376 # 'D'.upcast_for('D') -> 'D'
377 # 'D'.upcast_for('C') -> 'C[Float]'
378 # 'D'.upcast_for('B') -> 'C[String]'
379 # 'D'.upcast_for('A') -> 'A[String]'
380 fun upcast_for
(c
: MMLocalClass): MMType is abstract
382 # Return a type approximation if the reveiver is not self
383 # Useful for virtual types
384 fun not_for_self
: MMType do return self
386 # The nullable version of self (if needed)
387 var _as_nullable_cache
: nullable MMType = null
389 # IS the type can accept null?
390 fun is_nullable
: Bool do return false
392 # Return the nullable version of the type
393 # Noop if already nullable
394 fun as_nullable
: MMType do
395 var cache
= _as_nullable_cache
396 if cache
!= null then return cache
397 var res
= new MMNullableType(self)
398 _as_nullable_cache
= res
402 # Return the not null version of the type
403 # Noop if already not null
404 fun as_notnull
: MMType do return self
409 var _base_type
: MMType
410 redef fun is_valid
do return _base_type
.is_valid
411 redef fun is_nullable
: Bool do return true
412 redef fun as_notnull
do return _base_type
413 redef fun as_nullable
do return self
414 init(t
: MMType) do _base_type
= t
416 redef fun mmmodule
do return _base_type
.mmmodule
418 redef fun local_class
do return _base_type
.local_class
422 return t
isa MMNullableType and _base_type
< t
.as_notnull
427 return "nullable {_base_type}"
430 redef fun is_supertype
(t
)
432 return _base_type
.is_supertype
(t
)
435 redef fun for_module
(mod
)
437 return _base_type
.for_module
(mod
).as_nullable
440 redef fun adapt_to
(recv
)
442 return _base_type
.adapt_to
(recv
).as_nullable
445 redef fun upcast_for
(c
)
447 return _base_type
.upcast_for
(c
)
450 redef fun not_for_self
452 return _base_type
.not_for_self
.as_nullable
458 redef readable var _local_class
: MMLocalClass
459 redef fun mmmodule
do return _local_class
.mmmodule
end
460 redef fun <(t
) do return t
.is_supertype
(self)
464 return _local_class
.to_s
467 redef fun upcast_for
(c
)
470 if _local_class
!= c
then
471 t
= _local_class
.ancestor
(c
)
476 init(c
: MMLocalClass)
482 class MMTypeSimpleClass
484 redef fun is_supertype
(t
)
486 return t
.local_class
.cshe
<= _local_class
489 redef fun for_module
(mod
)
492 if mmmodule
!= mod
then
493 t
= _local_class
.for_module
(mod
).get_type
498 redef fun adapt_to
(recv
) do return self
500 init(c
: MMLocalClass)
509 redef readable var _mmmodule
: MMModule
510 redef fun is_nullable
: Bool do return true
511 redef fun <(t
) do return t
isa MMTypeNone or t
isa MMNullableType
512 redef fun to_s
do return "null"
513 redef fun is_supertype
(t
) do return false
514 redef fun local_class
do abort
515 redef fun upcast_for
(c
) do abort
516 redef fun as_nullable
do return self
517 redef fun as_notnull
do abort
519 private init(m
: MMModule) do _mmmodule
= m
524 readable var _type_none
: MMTypeNone = new MMTypeNone(self)
527 fun type_bool
: MMType
529 return class_by_name
(once
("Bool".to_symbol
)).get_type