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 attr _base_type_cache
: 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
34 # Register a new ancestor
35 protected meth 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 attr _ancestors
: Map[MMLocalClass, MMAncestor]
45 # The ancestor type for a given superclass
46 meth ancestor
(c
: MMLocalClass): MMType
48 assert _ancestors
!= null
49 if _ancestors
.has_key
(c
) then
50 return _ancestors
[c
].stype
56 redef class MMLocalProperty
57 # The signature of the property (where it is declared)
58 readable writable attr _signature
: MMSignature
60 attr _signatures_cache
: HashMap[MMType, MMSignature] = new HashMap[MMType, MMSignature]
62 # Return the adapted signature of self for a receiver of type t
63 meth signature_for
(t
: MMType): MMSignature do
64 if t
== local_class
.get_type
then return signature
66 if _signatures_cache
.has_key
(t
) then return _signatures_cache
[t
]
68 var res
= signature
.adaptation_to
(t
)
69 _signatures_cache
[t
] = res
74 # Signature for local properties
76 # The type of the reveiver
77 readable attr _recv
: MMType
80 attr _params
: Array[MMType]
83 readable attr _return_type
: MMType
85 # The closure parameters
86 readable attr _closures
: Array[MMClosure] = new Array[MMClosure]
88 # Number of parameters
91 assert _params
!= null
95 # Is self a valid subtype of an other signature
96 meth
<(s
: MMSignature): Bool
102 assert _recv
.module == s
.recv
.module
103 if arity
!= s
.arity
or (_return_type
== null) != (s
.return_type
== null) then return false
104 if _return_type
!= null and not _return_type
< s
.return_type
then
108 for i
in [0..arity
[ do
109 if not s
[i
] < self[i
] then
116 # The type of the i-th parameter
117 meth
[](i
: Int): MMType
119 assert _params
.length
> i
126 if _params
!= null and _params
.length
> 0 then
128 var a
= new Array[String].with_capacity
(_params
.length
)
129 for i
in [0.._params
.length
[ do
130 #var pn = _params_name[i]
135 s
= "({a.join(",")})"
139 if _return_type
!= null then
140 s
.append
(": {_return_type}")
145 # Adapt the signature to a different receiver
146 meth adaptation_to
(r
: MMType): MMSignature
154 p
= new Array[MMType]
156 p
.add
(i
.for_module
(mod
).adapt_to
(r
))
159 var rv
= _return_type
161 rv
= rv
.for_module
(mod
).adapt_to
(r
)
163 var res
= new MMSignature(p
,rv
,r
)
164 for clos
in _closures
do
165 res
.closures
.add
(clos
.adaptation_to
(r
))
170 attr _not_for_self_cache
: MMSignature
172 # Return a type approximation if the reveiver is not self
173 # Useful for virtual types
174 meth not_for_self
: MMSignature
176 var res
= _not_for_self_cache
177 if res
!= null then return res
179 var need_for_self
= false
182 p
= new Array[MMType]
184 var i2
= i
.not_for_self
185 if i
!= i2
then need_for_self
= true
186 p
.add
(i
.not_for_self
)
190 var rv
= _return_type
192 var rv
= rv
.not_for_self
193 if rv
!= _return_type
then need_for_self
= true
196 if need_for_self
then
197 res
= new MMSignature(p
, rv
, _recv
)
198 for clos
in _closures
do
199 res
.closures
.add
(clos
.not_for_self
)
205 _not_for_self_cache
= res
209 init(params
: Array[MMType], return_type
: MMType, r
: MMType)
211 assert params
!= null
213 _return_type
= return_type
218 # A closure in a signature
220 # The signature of the closure
221 readable attr _signature
: MMSignature
223 # Is the closure a brek one
224 # aka is defined with the break keyword thus does not return
225 readable attr _is_break
: Bool
227 # Adapt the signature to a different receiver
228 meth adaptation_to
(r
: MMType): MMClosure
230 return new MMClosure(_signature
.adaptation_to
(r
), _is_break
)
233 init(s
: MMSignature, is_break
: Bool)
239 meth not_for_self
: MMClosure
241 return new MMClosure(_signature
.not_for_self
, _is_break
)
245 # Inheritance relation between two types
246 abstract class MMAncestor
248 readable writable attr _stype
: MMType = null
250 # The inheriter (heir) type
251 readable writable attr _inheriter
: MMType = null
253 meth is_reffinement
: Bool do
254 return stype
.module != stype
.module
257 meth is_specialisation
: Bool do
258 return stype
.local_class
.global
!= inheriter
.local_class
.global
261 # The inherited class
262 meth local_class
: MMLocalClass is abstract
266 if stype
== null then
267 return local_class
.to_s
275 # Note that static type a related to a specific module
276 abstract class MMType
277 # The module where self makes sence
278 meth
module: MMModule is abstract
280 # The local class that self direclty or indirectly refers to
281 meth local_class
: MMLocalClass is abstract
283 # Is self a valid subtype of t
284 meth
<(t
: MMType): Bool is abstract
286 # Is self a valid supertype of t
287 # This method must be only called within definition of < if
288 # a double dispatch is needed
289 meth is_supertype
(t
: MMType): Bool is abstract
291 # Adapt self to another module
292 meth for_module
(mod
: MMModule): MMType is abstract
294 # Get the type adapted to another receiver type
295 # Useful for formal types
296 meth adapt_to
(recv
: MMType): MMType is abstract
298 # Adapt self to another local class context
299 # Useful for genericity
300 meth upcast_for
(c
: MMLocalClass): MMType is abstract
302 # Return a type approximation if the reveiver is not self
303 # Useful for virtual types
304 meth not_for_self
: MMType do return self
309 redef readable attr _local_class
: MMLocalClass
310 redef meth
module do return _local_class
.module end
311 redef meth
<(t
) do return t
!= null and t
.is_supertype
(self)
315 return _local_class
.to_s
318 redef meth upcast_for
(c
)
320 assert _local_class
!= null
324 if _local_class
!= c
then
325 t
= _local_class
.ancestor
(c
)
331 init(c
: MMLocalClass)
337 class MMTypeSimpleClass
339 redef meth is_supertype
(t
)
341 return t
.local_class
.cshe
<= _local_class
344 redef meth for_module
(mod
)
347 if module != mod
then
348 t
= _local_class
.for_module
(mod
).get_type
354 redef meth adapt_to
(recv
) do return self
356 init(c
: MMLocalClass)
365 redef readable attr _module
: MMModule
366 redef meth
<(t
) do return true
367 redef meth is_supertype
(t
) do return false
368 redef meth local_class
do abort
369 redef meth upcast_for
(c
) do return self
371 private init(m
: MMModule) do _module
= m
376 readable attr _type_none
: MMTypeNone = new MMTypeNone(self)