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 # Return the adapted signature of self for a receiver of type t
61 meth signature_for
(t
: MMType): MMSignature do
63 assert x
isa MMConcreteProperty
64 return x
.signature
.adaptation_to
(t
)
68 # Signature for local properties
70 # The type of the reveiver
71 readable attr _recv
: MMType
74 attr _params
: Array[MMType]
77 readable attr _return_type
: MMType
79 # Number of parameters
82 assert _params
!= null
86 # Is self a valid subtype of an other signature
87 meth
<(s
: MMSignature): Bool
93 assert _recv
.module == s
.recv
.module
94 assert arity
== s
.arity
95 assert (_return_type
== null) == (s
.return_type
== null)
96 if _return_type
!= null and not _return_type
< s
.return_type
then
100 for i
in [0..arity
[ do
101 if not s
[i
] < self[i
] then
108 # The type of the i-th parameter
109 meth
[](i
: Int): MMType
111 assert _params
.length
> i
118 if _params
!= null and _params
.length
> 0 then
120 var a
= new Array[String].with_capacity
(_params
.length
)
121 for i
in [0.._params
.length
[ do
122 #var pn = _params_name[i]
127 s
= "({a.join(",")})"
131 if _return_type
!= null then
132 s
.append
(": {_return_type}")
137 # Adapt the signature to a different receiver
138 meth adaptation_to
(r
: MMType): MMSignature
146 p
= new Array[MMType]
148 p
.add
(i
.for_module
(mod
).adapt_to
(r
))
151 var rv
= _return_type
153 rv
= rv
.for_module
(mod
).adapt_to
(r
)
155 return new MMSignature(p
,rv
,r
)
158 init(params
: Array[MMType], return_type
: MMType, r
: MMType)
160 assert params
!= null
162 _return_type
= return_type
167 # Inheritance relation between two types
168 abstract class MMAncestor
170 readable writable attr _stype
: MMType
172 # The inheriter (heir) type
173 readable writable attr _inheriter
: MMType
175 meth is_reffinement
: Bool do
176 return stype
.module != stype
.module
179 meth is_specialisation
: Bool do
180 return stype
.local_class
.global
!= inheriter
.local_class
.global
183 # The inherited class
184 meth local_class
: MMLocalClass is abstract
188 if stype
== null then
189 return local_class
.to_s
197 # Note that static type a related to a specific module
198 abstract class MMType
199 # The module where self makes sence
200 meth
module: MMModule is abstract
202 # The local class that self direclty or indirectly refers to
203 meth local_class
: MMLocalClass is abstract
205 # Is self a valid subtype of t
206 meth
<(t
: MMType): Bool is abstract
208 # Is self a valid supertype of t
209 # This method must be only called within definition of < if
210 # a double dispatch is needed
211 meth is_supertype
(t
: MMType): Bool is abstract
213 # Select a method from its name
214 meth select_method
(name
: Symbol): MMMethod
216 assert local_class
!= null
218 var res
= select_property
(local_class
.method
(name
))
219 assert res
isa MMMethod
223 # Select an attribute from its name
224 meth select_attribute
(name
: Symbol): MMAttribute
227 assert local_class
!= null
228 var res
= select_property
(local_class
.attribute
(name
))
229 assert res
isa MMAttribute
233 # Select a local property from its global property
234 meth select_property
(t
: MMGlobalProperty): MMLocalProperty is abstract
236 # Adapt self to another module
237 meth for_module
(mod
: MMModule): MMType is abstract
239 # Get the type adapted to another receiver type
240 # Useful for formal types
241 meth adapt_to
(recv
: MMType): MMType is abstract
243 # Adapt self to another local class context
244 # Useful for genericity
245 meth upcast_for
(c
: MMLocalClass): MMType is abstract
247 # Return a type approximation if the reveiver is not self
248 # Useful for virtual types
249 meth not_for_self
: MMType do return self
254 redef readable attr _local_class
: MMLocalClass
255 redef meth
module do return _local_class
.module end
256 redef meth
<(t
) do return t
!= null and t
.is_supertype
(self)
260 return _local_class
.to_s
263 redef meth upcast_for
(c
)
265 assert _local_class
!= null
269 if _local_class
!= c
then
270 t
= _local_class
.ancestor
(c
)
276 init(c
: MMLocalClass)
282 class MMTypeSimpleClass
284 redef meth is_supertype
(t
)
286 return t
.local_class
.cshe
<= _local_class
289 redef meth select_property
(g
)
291 assert _local_class
!= null
295 return _local_class
[g
]
298 redef meth for_module
(mod
)
301 if module != mod
then
302 t
= _local_class
.for_module
(mod
).get_type
308 redef meth adapt_to
(recv
) do return self
310 redef init(c
: MMLocalClass)
319 redef readable attr _module
: MMModule
320 redef meth
<(t
) do return true
321 redef meth is_supertype
(t
) do return false
322 redef meth local_class
do abort
323 redef meth upcast_for
(c
) do return self
325 private init(m
: MMModule) do _module
= m
330 readable attr _type_none
: MMTypeNone = new MMTypeNone(self)